5
5

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.

hubotからBotkitへ移行メモ

Posted at

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 })
  })
})
5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?