hubotからBotkitへ移行メモ


hubotからBotkitへ移行したときの移行メモ
(Botkitの基本は省略)

robot.respond

robot.respond(/ほげ (.+)$/, (msg) => {
  console.log(msg.match[1])
})

controller.hears('ほげ (.+)$', 'mention,direct_mention', (bot, msg) => {
  console.log(msg.match[1])
})

'mention,direct_mention' のところは厳密に同じにするなら'mention,direct_mention,direct_message

robot.hear

robot.hear(/ほげ (.+)$/, (msg) => {
  //...
})

controller.hears('ほげ (.+)$', 'ambient', (bot, msg) => {
  console.log(msg.match[1])
})

ambient のところを message_received としている記事が多かったが、@0.4.10では動かなかった。
厳密に同じにするならambient,mention,direct_mention,direct_message

robot.messageRoom

robot.messageRoom(channelId, 'わーい')

bot.say({
  channel: channelId,
  text: 'わーい'
})

text内のURL

Botkitではmsg.textでURLを含んだテキストを参照すると、URLの前後に<>がついている。
正規表現等で考慮していなければ修正する。

ユーザー名取得

msg.message.user.name

bot.api.users.info({ user: msg.user }, (error, response) => {
  const { name } = response.user
})

非同期になるのでやや修正大きい。

attachment

msg.send({
  attachments: [attachment]
})

bot.reply(msg, {
  attachments: [attachment]
})

robot.router

robot.router.post("/room/:room", function(req, res) {
  //...
  robot.send({ room: req.params.room }, text)
})
const bot = controller.spawn({
  token: process.env.token
})
controller.setupWebserver(port, (err, webserver) => {
  webserver.post('/room/:channel', (req, res) => {
    bot.say({ text, channel: req.params.channel })
  })
})