简介

Hubot是Github开发并开源的chatbot,但它并不仅仅是一个聊天机器人,Hubot已经广泛应用于Github的日常运维工作,被称为最忙碌的员工
https://scarletsky.github.io/2016/05/02/write-your-own-hubot-scripts/

安装

https://github.com/hubotio/hubot

npm install -g yo generator-hubot

生成slack适配机器人

https://github.com/slackapi/hubot-slack

mkdir myhubot
cd myhubot
yo hubot --adapter=slack

启动

HUBOT_SLACK_TOKEN=xxx ./bin/hubot --adapter slack

API

  • robot.hear
  • res.send
  • robot.respond
  • res.reply
  • res.match
  • robot.listen
  • robot.http
  • robot.hear /error
  • robot.hear /event
  • robot.brain
    • data.users

res

  • res.message

    TextMessage {
    user:
    User {
    _modelName: 'User',
    id: 'U52CTM3M0',
    team_id: 'T4BFENTSR',
    name: 'wujiyu',
    deleted: false,
    color: '674b1b',
    real_name: 'jiyu wu',
    tz: 'Asia/Chongqing',
    tz_label: 'China Standard Time',
    tz_offset: 28800,
    profile:
    { first_name: 'jiyu',
    last_name: 'wu',
    avatar_hash: 'gc19ce1b315f',
    real_name: 'jiyu wu',
    real_name_normalized: 'jiyu wu',
    email: 'wujiyu115@gmail.com',
    image_24: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0000-24.png',
    image_32: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0000-32.png',
    image_48: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0000-48.png',
    image_72: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0000-72.png',
    image_192: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0000-192.png',
    image_512: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0000-512.png',
    fields: null,
    team: 'T4BFENTSR' },
    is_admin: false,
    is_owner: false,
    is_primary_owner: false,
    is_restricted: false,
    is_ultra_restricted: false,
    is_bot: false,
    updated: 1492741610,
    presence: 'active',
    room: 'D52FTG4KX' },
    text: 'robot help',
    id: '1501126623.796631',
    done: false,
    room: 'D52FTG4KX',
    thread_ts: undefined }
  • res.envelope

    { room: 'D52FTG4KX',
    user:
    User {
    _modelName: 'User',
    id: 'U52CTM3M0',
    team_id: 'T4BFENTSR',
    name: 'wujiyu',
    deleted: false,
    color: '674b1b',
    real_name: 'jiyu wu',
    tz: 'Asia/Chongqing',
    tz_label: 'China Standard Time',
    tz_offset: 28800,
    profile:
    { first_name: 'jiyu',
    last_name: 'wu',
    avatar_hash: 'gc19ce1b315f',
    real_name: 'jiyu wu',
    real_name_normalized: 'jiyu wu',
    email: 'wujiyu115@gmail.com',
    image_24: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0000-24.png',
    image_32: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0000-32.png',
    image_48: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0000-48.png',
    image_72: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0000-72.png',
    image_192: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0000-192.png',
    image_512: 'https://secure.gravatar.com/avatar/c19ce1b315f61749143b4717069bcd5a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0000-512.png',
    fields: null,
    team: 'T4BFENTSR' },
    is_admin: false,
    is_owner: false,
    is_primary_owner: false,
    is_restricted: false,
    is_ultra_restricted: false,
    is_bot: false,
    updated: 1492741610,
    presence: 'active',
    room: 'D52FTG4KX' },
    message:
    TextMessage {
    user:
    User {
    _properties: [Object],
    _modelName: 'User',
    id: 'U52CTM3M0',
    team_id: 'T4BFENTSR',
    name: 'wujiyu',
    deleted: false,
    color: '674b1b',
    real_name: 'jiyu wu',
    tz: 'Asia/Chongqing',
    tz_label: 'China Standard Time',
    tz_offset: 28800,
    profile: [Object],
    is_admin: false,
    is_owner: false,
    is_primary_owner: false,
    is_restricted: false,
    is_ultra_restricted: false,
    is_bot: false,
    updated: 1492741610,
    presence: 'active',
    room: 'D52FTG4KX' },
    text: 'robot help',
    id: '1501126623.796631',
    done: false,
    room: 'D52FTG4KX',
    thread_ts: undefined } }

slack api

https://api.slack.com/bot-users#api_usage

侦听回应

module.exports = (robot) ->
robot.hear /badger/i, (res) ->
# your code here
robot.respond /open the pod bay doors/i, (res) ->