12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

slackからAWS SNS経由でpush通知を送る

Posted at

誰でも簡単にpush通知が送れるようになったら良いかと思いslackとhubotでAWS SNSを操作してみた。
SNSでAppの登録とEndpointが登録されていることが前提。

AppからEndpointを取り出して新規Topicに入れる

  • slcakからはhubot sns createとかで作れるようにする
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)
        )
    )

作成したTopicに対してpush通知を送る

  • slcakからはhubot sns push <topicArn> <message>とかで送れるようにする
  • topicArnは上で作った(botが返す値)を入れる
  • 送信し終わったTopicは削除しておく
  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```"
        )
    )
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?