Commit c5980417 authored by Aleix Quintana Alsius's avatar Aleix Quintana Alsius

Detect custom command in multiline and replies. Format command:ls in plain...

Detect custom command in multiline and replies. Format command:ls in plain text also. Allow plain_text in sendMessage
parent e8e85c35
......@@ -279,20 +279,28 @@ async function handleMessage (event){
webhookCommandStore.list(event.getRoomId()).then(
(rows, reject) => {
const commands_list = [];
const commands_list_text = [];
const commands_list_admin = [];
const commands_list_admin_text = [];
if(reject) return reject;
rows.forEach(function (row) {
commands_list.push( `|${row.custom_command}|${row.url}|${row.context}|`);
commands_list.push(`|${row.custom_command}|${row.url}|${row.context}|`);
commands_list_text.push(`custom_command:\t${row.custom_command}\nurl:\t${row.url}\ncontext:${row.context}\n`);
commands_list_admin.push(`|${row.custom_command}|${row.url}|${row.context}|${row.token}|`);
commands_list_admin_text.push(`custom_command:\t${row.custom_command}\nurl:\t${row.url}\ncontext:\t${row.context}\ntoken:\t${row.token}\n`);
});
const answer = md.render("This is the list of available commands in this room:\n" +
"| custom_command | url | context |\n |---|---|---|\n"+
commands_list.join("\n") + "\n");
const answer_text = "This is the list of available commands in this room:\n" +
commands_list_text.join("------------------------------\n") + "\n";
const answer_admin = md.render("This is the list of available commands in this room with tokens:\n" +
"| custom_command | url | context | token |\n |---|---|---|---|\n"+
commands_list_admin.join("\n") + "\n");
sendNotice(event.getRoomId(), answer);
sendDirectNotice(event.getSender(), event.getRoomId(), answer_admin);
const answer_admin_text = "This is the list of available commands in this room with tokens:\n" +
commands_list_admin_text.join("------------------------------\n") + "\n";
sendNotice(event.getRoomId(), answer, answer_text);
sendDirectNotice(event.getSender(), event.getRoomId(), answer_admin, answer_admin_text);
}
);
}
......@@ -326,14 +334,18 @@ async function handleMessage (event){
webhookCommandStore.list(event.getRoomId()).then(
(rows, reject) => {
const commands_list = [];
const commands_list_text = [];
if(reject) return reject;
rows.forEach(function (row) {
commands_list.push( `|${row.custom_command}|${row.url}|${row.context}|`);
commands_list_text.push( `custom_command:\t${row.custom_command}\nurl:\t${row.url}\ncontext:${row.context}\n`);
});
const answer = md.render("This is the list of available commands in this room:\n" +
"| custom_command | url | context |\n |---|---|---|\n"+
commands_list.join("\n") + "\n");
sendNotice(event.getRoomId(), answer);
const answer_text = "This is the list of available commands in this room:\n" +
commands_list_text.join("------------------------------\n") + "\n";
sendNotice(event.getRoomId(), answer, answer_text);
}
);
}
......@@ -341,11 +353,13 @@ async function handleMessage (event){
return;
}
// Custom command runner
if ( event.getContent().body.startsWith('!') || event.getContent().body.match(/!\S+/) ){
const lookup_command = event.getContent().body.match(/(\n|)(!\S+)/)[2];
debugger
if ( event.getContent().body.startsWith('!') || event.getContent().body.match(/(^\s*(!\S)+)|(\\n(\s)*(!\S)+)/m) ){
const lookup_command = event.getContent().body.match(/(^\s*(!\S)+)|(\\n(\s)*(!\S)+)/m)[2]||event.getContent().body.match(/(^\s*(!\S)+)|(\\n(\s)*(!\S)+)/m)[5];
debugger
webhookCommandStore.commandLookup(event.getRoomId(),lookup_command).then((custom_command)=>{
if (custom_command) {
logger.debug(lookup_command + "is in commandStore let's run it.");
logger.debug(lookup_command + " is in commandStore let's run it.");
const result = httpClient.commandRequest(lookup_command, custom_command.url, custom_command.context, custom_command.token, event).then((response) => {
// Expecting application/json
if (!response.headers['content-type'].startsWith('application/json') &&
......@@ -454,8 +468,10 @@ async function autoVerify(room_id){
* The room id for which this administrative task is performed
* @param {string} body
* The body of the message wishing to be sent.
* @param {string} body_text
* The body of the message wishing to be sent in plain text.
*/
async function sendDirectNotice(user_id, room_id, body){
async function sendDirectNotice(user_id, room_id, body, body_text){
// find invited direct message rooms
// Would be nice to restrict for the ones having m.direct from account data
const mdirect_events = client.getAccountData('m.direct');
......@@ -475,7 +491,7 @@ async function sendDirectNotice(user_id, room_id, body){
&& loaded_room.getInvitedAndJoinedMemberCount() == 2
});
if (invited_dm_rooms[0]) {
sendNotice(invited_dm_rooms[0], body)
sendNotice(invited_dm_rooms[0], body, body_text)
} else {
client.createRoom({
preset: 'trusted_private_chat',
......@@ -494,10 +510,12 @@ async function sendDirectNotice(user_id, room_id, body){
* The room id for which this administrative task is performed
* @param {string} body
* The body of the message wishing to be sent.
* @param {string} body_text
* The body of the message wishing to be sent in plain text.
*/
async function sendNotice(room_id, body) {
async function sendNotice(room_id, body, body_text) {
var content = {
"body" : stripHtml(body),
"body" : body_text || stripHtml(body),
"format": "org.matrix.custom.html",
"formatted_body" : body,
"msgtype" : "m.notice"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment