Skip to content

Instantly share code, notes, and snippets.

@mlopezr
Created September 6, 2016 23:52
Show Gist options
  • Save mlopezr/2ed143489024da55a1226e71265e9a67 to your computer and use it in GitHub Desktop.
Save mlopezr/2ed143489024da55a1226e71265e9a67 to your computer and use it in GitHub Desktop.
Zapier script to integrate with matrix.org chatrooms
'use strict';
var Zap = {
get_session_info: function(bundle) {
/*
Argument:
bundle.request.url: <string>
bundle.request.headers: <object>
bundle.request.params: <object> # this will be mapped into the querystring
bundle.request.data: <object>
bundle.auth_fields: <object>
bundle.zap: <object> # info about the zap
*/
var access_token,
login_request,
login_response,
parsed_response;
// Assemble the login request
login_request = {
method: 'POST',
url: bundle.auth_fields.server + '/_matrix/client/r0/login',
json: {
"type": "m.login.password",
"user": bundle.auth_fields.username,
"password": bundle.auth_fields.password
},
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
};
console.log("Matrix app trying to log in with username " + bundle.auth_fields.username + ' on server ' + bundle.auth_fields.server);
// Fire off the login request
login_response = z.request(login_request);
if (!login_response) {
throw new HaltedException('Error in login request');
}
console.log("Response from Matrix server:");
console.log(login_response);
parsed_response = JSON.parse(JSON.stringify(login_response.content));
if (!parsed_response.access_token) {
throw new HaltedException('Login request does not have access token. Probably invalid login credentials');
}
access_token = parsed_response.access_token;
console.log("Matrix app successfully retrieved access token " + access_token);
// This will be mixed into bundle.auth_fields in future calls.
return {'access_token': access_token};
},
// --------------------------------------------------------------------------------------------
msgOut_write: function(bundle) {
var msg_out_request_payload,
txnId,
response,
parsed_response,
request_url,
event_id;
var crypto = require('crypto');
txnId = crypto.randomBytes(6).toString('hex');
console.log("Matrix app entering msgOut_write with txnId " + txnId + " and auth_fields:");
console.log(bundle.auth_fields);
if (!bundle.auth_fields.access_token) {
throw new InvalidSessionException('No access token');
}
request_url = bundle.auth_fields.server + '/_matrix/client/r0/rooms/' + bundle.action_fields_full.roomId + '/send/m.room.message/' + txnId + '?access_token=' + bundle.auth_fields.access_token;
console.log('Sending PUT to URL ' + request_url);
msg_out_request_payload = {
method: 'PUT',
url: request_url,
json: {
"msgtype": "m.text",
"body": bundle.action_fields_full.message},
headers: {
'Content-Type': 'application/json',
Accept: 'application/json'
}
};
// Fire off the request
response = z.request(msg_out_request_payload);
console.log("Response from Matrix server:");
console.log(response);
if (!response || !response.status_code) {
throw new ErrorException('Request failed. Unknown status code');
}
if (response.status_code >= 300) {
if (response.errcode) {
if (response.errcode === 'M_MISSING_TOKEN') {
throw new InvalidSessionException();
} else {
throw new ErrorException('Server sent error: '+ response.error);
}
}
else {
throw new ErrorException('Request failed. Status > 300 but no error code');
}
} else {
// Parse response content
parsed_response = JSON.parse(JSON.stringify(response.content));
// Extract the `event_id` from returned JSON
event_id = parsed_response.event_id;
return {'event_id': event_id};
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment