Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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