Skip to content

Instantly share code, notes, and snippets.

@kaz29
Last active December 12, 2016 04:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaz29/613812c68cb26962112484d94af323fa to your computer and use it in GitHub Desktop.
Save kaz29/613812c68cb26962112484d94af323fa to your computer and use it in GitHub Desktop.
Azure functions Chatwork to Slack
var request = require('request');
var Slack = require('slack-node');
module.exports = function (context, myTimer) {
var timeStamp = new Date().toISOString();
if(myTimer.isPastDue)
{
context.log('JavaScript is running late!');
}
context.log('JavaScript timer trigger function ran!', timeStamp);
var chatid = process.env['CW_CHATID'];
var options = {
method: 'GET',
uri: 'https://api.chatwork.com/v1/rooms/' + chatid + '/messages',
timeout: 30 * 1000,
headers: {
'X-ChatWorkToken': process.env['CW_TOKEN']
},
};
request(options, function(error, response) {
context.log("inside request callback");
if (response.statusCode == 204) {
context.done();
return;
}
if (error || response.statusCode != 200) {
context.log('error: ' + error);
context.done();
return;
}
slack = new Slack();
slack.setWebhook(process.env['SLACK_WEBHOOK_URI']);
messages = JSON.parse(response.body);
messages.forEach(function(message) {
text = '*' + message.account.name + '* sent message by Chatwork.\n' + message.body + '\n';
text = text + '<https://www.chatwork.com/#!rid' + chatid + '-' + message.message_id + '| Link to ChatWork>';
slack.webhook({
channel: process.env['SLACK_CHANNEL'],
username: "Chatwork Bot",
text: text
}, function(err, response) {
context.log(response);
});
});
context.done();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment