Created
December 18, 2014 19:48
-
-
Save hotakasaito/a2e75be62016ad1c0428 to your computer and use it in GitHub Desktop.
slackからAWS SNS経由でpush通知を送る ref: http://qiita.com/hotakasaito/items/620247240b2f76e58e0e
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
AWS = require('aws-sdk') | |
AWS.config.region = 'ap-northeast-1' | |
sns = new AWS.SNS() | |
platformApplicationArn = 'arn:aws:sns:ap-northeast-1:xxx:xxx' | |
module.exports = (robot) -> | |
Date::format = -> | |
"#{@.getFullYear()}#{@.getMonth() + 1}#{@.getDate()}#{@.getHours()}#{@.getMinutes()}#{@.getSeconds()}" | |
subscribe = (topicArn, endpointArn) -> | |
sns.subscribe({TopicArn: topicArn, Protocol: 'application', Endpoint: endpointArn}, (err, data) -> | |
if err | |
console.log(err, err.stack) | |
else | |
console.log(data) | |
) | |
robot.respond /\s+(?:sns)\s+create\s*/i, (msg) -> | |
# topicを作ったのが誰だか分かるようにした | |
topicName = "push-notifications-topic-#{msg.message.user.name}-#{new Date().format()}" | |
sns.createTopic({Name: topicName}, (err, data) -> | |
if err | |
console.log(err, err.stack) | |
msg.send err | |
return | |
else | |
topicArn = data.TopicArn | |
sns.listEndpointsByPlatformApplication({PlatformApplicationArn: platformApplicationArn}).eachPage((err, data) -> | |
if err | |
console.log(err, err.stack) | |
msg.send err | |
else if !data | |
msg.send "create success\n```\n#{topicArn}\n```" | |
else | |
for endpoint in data.Endpoints | |
# 無効なトークンは除外 | |
continue if endpoint.Attributes.Enabled == 'false' | |
subscribe(topicArn, endpoint.EndpointArn) | |
) | |
) |
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
robot.respond /\s+sns\s+push\s+(\S+)\s+[\'|\"]?(.+?)[\"|\']?$/i, (msg) -> | |
# topicArnを簡易チェック(自分が作ったTopicしか操作出来ないようにした) | |
if ! ///push-notifications-topic-#{msg.message.user.name}-///i.test(msg.match[1]) | |
msg.send "error: topicArn(#{msg.match[1]})" | |
return | |
topicArn = msg.match[1] | |
params = { | |
TopicArn: topicArn, | |
Message: msg.match[2] | |
} | |
sns.publish(params, (err, data) -> | |
if err | |
console.log(err, err.stack) | |
msg.send err | |
else | |
message_id = data.MessageId | |
sns.deleteTopic({TopicArn: topicArn}, (err, data) -> | |
if err | |
console.log(err, err.stack) | |
msg.send err | |
else | |
msg.send "push success\n```\n#{message_id}\n```" | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment