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

Detect custom command in replies. Posting JSON architecture.

parent 501811be
......@@ -2,7 +2,21 @@
This bot will allow to room administrators and moderators to generate room custom commands (in a similar way how telegram commands are thought).
When they are invoked will post the message event object data and a predefined context object along with a token to a predefined url. If the url where it posts returns a JSON with the same token and a text:
When they are invoked will post the message event object data and a predefined context object along with a token to a predefined url:
```
Authorization: Bearer TTTTOOOKKKEENNNXXXXXXXXX
Content-type: application/json;charset=UTF-8;
{
"text" : {
"event" : { The MATRIX MESSAGE EVENT IN JSON }
"context" : { The constant context predefined when creating the custom command }
}
}
```
If the url where it posts returns a JSON with the same token and a text:
```
{
......
......@@ -341,31 +341,36 @@ async function handleMessage (event){
return;
}
// Custom command runner
if ( event.getContent().body.startsWith('!')){
const lookup_command = event.getContent().body.replace(/ .*/,'');
if ( event.getContent().body.startsWith('!') || event.getContent().body.match(/!\S+/) ){
const lookup_command = event.getContent().body.match(/(\n|)(!\S+)/)[2];
webhookCommandStore.commandLookup(event.getRoomId(),lookup_command).then((custom_command)=>{
if (custom_command) {
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') || !response.headers['content-type'].startsWith('application/vnd.api+json')){
logger.log("Expecting a json response, while it is" + response.headers['content-type']);
sendNotice(event.getRoomId(), "Could not parse the answer from external web.");
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') &&
!response.headers['content-type'].startsWith('application/vnd.api+json')
){
logger.log("Expecting a json response, while it is" + response.headers['content-type']);
sendNotice(event.getRoomId(), "Could not parse the answer from external web. Invalid content-type");
return;
}
debugger
// We are looking for data.text to be sent to the room and for the existance of custom_command.token ...
// A little silly but someday will be better... TODO
if ((response.headers['authorization']|| "").substring(7) != custom_command.token){
sendNotice(event.getRoomId(), "Could not parse the answer from external web. Unauthorized response.");
return;
}
// We are looking for data.text to be sent to the room
console.log( response.data.text );
logger.debug(response.status);
logger.debug(response.headers['content-type']);
sendNotice(event.getRoomId(), response.status.toString());
//
})
.catch((error)=> {
logger.error(error);
const answer = md.render("**" + error.response.status + "** : " + error.response.statusText +
": Could not parse the answer from external web.");
sendNotice(event.getRoomId(), answer);
});
logger.debug(response.status);
sendNotice(event.getRoomId(), (response.data || { text : response.status.toString() } ).text );
})
.catch((error)=> {
logger.error(error);
const answer = md.render("**" + error.response.status + "** : " + error.response.statusText +
": Could not parse the answer from external web.");
sendNotice(event.getRoomId(), answer);
});
}
});
}
......
......@@ -24,7 +24,9 @@ class HttpClient {
try {
const config = {
headers: {
Authorization: `Bearer ${token}`
Authorization: `Bearer ${token}`,
Accept: 'application/json',
'Content-Type': 'application/json'
}
};
const body = {
......
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