Last active
December 12, 2016 04:29
Star
You must be signed in to star a gist
Azure functions Chatwork to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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