💬

復活のHubot?

2024/06/29に公開

Hubotとは

https://github.com/hubotio/hubot

SlackやRocket.Chat, XMPPといったチャットサービスに接続し、投稿された内容に応じてアクションを行ったり、自ら投稿を行ったりすることができるフレームワーク。

Github社が開発して公開しているもの。

CoffeeScriptというJavaScriptにコンパイルして利用する、独特な記法の言語で開発され、利用する際にもCoffeeScriptで利用するというフレームワークでした。

2011年10月26日に v1.0.0 がリリースされ、日本では2016年〜2017年頃にブームを迎え、その後いつの間にか忘れされ、CoffeeScriptとともに表舞台から消え去っていたように思います。

圧倒的なシェアを持つSlackが2019年に公式にBoltというフレームワークを提供開始したことも影響しているのかなと思っていますがわかりません。

https://github.com/slackapi/bolt-js

Google Trend

最近の状況

GitHubで見ると、2019年4月17日の v3.3.2 を最後に、メンテナンスが完全に停止していました。

しかし、突如 2023年4月18日に v3.4.0 がリリースされ、そこから怒涛の開発がなされていました。

メジャーアップデートだけ見るとこんな感じです。

2019-04-17 v3.3.2
  ... 4年間の空白 ...
2023-05-13 v4.0.0
2023-06-10 v5.0.0
2023-07-24 v6.0.0
2023-07-29 v7.0.0
2023-08-13 v8.0.0
2023-09-23 v9.0.0
2023-11-05 v10.0.0
2023-11-12 v11.0.0
2024-05-30 v11.2.0(現在の最新版)

そして、v11.0.0をもって完全にCoffeeScriptが廃止され、全コードがECMAScriptになっています。

最新版を使ってみる

Getting Started With Hubot

インストール

% mkdir v11
% cd v11
% npm init
% npm install hubot

botの作成

% npx hubot --create myhubot
creating hubot directory myhubot
npm init
npm i
added 3 packages, and audited 119 packages in 2s

15 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

package.json updated successfully.
.env file not found, continuing to the next operation.

package.jsonにショートカットを登録しておく(と便利)

package.json
{
...
    "scripts": {
        "start": "hubot"
    }
...
}

これで

% npm start

とするだけで起動できるようになります。

起動してみる

% npm start

> v11@1.0.0 start
> hubot
Hubot> 

help

Hubot> help
usage:
\q, exit - close Shell and exit
\?, help - show this help
\c, clear - clear screen
Hubot> 
Hubot> exit

botの名前を指定して起動

% npx hubot --name sam
sam> 

コミュニティのスクリプト利用

hubotではコミュニティでメンテナンスされている多くのスクリプトがあり簡単に利用することができるとのことです。

以下で検索可能です。

% npm search hubot-scripts github
hubot-github-deploymentshubot-github-deployments
Integrate with GitHub deployment APIIntegrate with GitHub deployment API
Version 1.4.1 published 2024-03-30 by stephenyeargin
Maintainers: stephenyeargin
Keywords: hubot hubot-scripts github deployment deployment api
https://npm.im/hubot-github-deployments

hubot-github-comparehubot-github-compare
Return Github comparison URLs for two branches/releases on any repoReturn Github comparison URLs for two branches/releases on any repo
Version 1.1.0 published 2015-05-16 by markhuge
Maintainers: markhuge
Keywords: hubot hubot-scripts github
https://npm.im/hubot-github-compare

hubot-github-repo-event-notifierhubot-github-repo-event-notifier
Notifies about any available GitHub repo event via webhookNotifies about any available GitHub repo event via webhook
Version 1.8.1 published 2018-03-27 by strugee
Maintainers: parkr patcon strugee
Keywords: hubot hubot-scripts notifier github events webhooks
https://npm.im/hubot-github-repo-event-notifier

hubot-github-create-pullrequestshubot-github-create-pullrequests
Open Github pull requests with HubotOpen Github pull requests with Hubot
Version 1.1.1 published 2014-11-22 by summera
Maintainers: summera
Keywords: hubot hubot-scripts hubot-github github
https://npm.im/hubot-github-create-pullrequests

hubot-github-tfshubot-github-tfs
A Hubot script to integrate GitHub and Microsoft Team Foundation Server (TFS)A Hubot script to integrate GitHub and Microsoft Team Foundation Server (TFS)
Version 0.9.4 published 2016-06-10 by helaili
Maintainers: helaili
Keywords: hubot hubot-scripts tfs
https://npm.im/hubot-github-tfs

hubot-vsphere-commandshubot-vsphere-commands
A hubot script that allows users to interact with the vsphere-rest-api developed by quickjp2 on github.A hubot script that allows users to interact with the vsphere-rest-api developed by quickjp2 on github.
Version 1.5.2 published 2017-12-22 by quickjp2
Maintainers: quickjp2
Keywords: hubot hubot-scripts vmware
https://npm.im/hubot-vsphere-commands

hubot-whats-newhubot-whats-new
A hubot script that lists all commits since the latest (or given) version/tagA hubot script that lists all commits since the latest (or given) version/tag
Version 1.1.0 published 2017-06-18 by evenbrenna
Maintainers: evenbrenna
Keywords: hubot hubot-scripts github git vcs history commit
https://npm.im/hubot-whats-new

hubot-github-review-reminderhubot-github-review-reminder
Remind the assigned review of githubRemind the assigned review of github
Version 0.3.0 published 2018-06-25 by osdakira
Maintainers: osdakira
Keywords: hubot hubot-scripts
https://npm.im/hubot-github-review-reminder

hubot-github-pull-request-creatorhubot-github-pull-request-creator
Create pull-request on githubCreate pull-request on github
Version 0.0.0 published 2018-06-06 by osdakira
Maintainers: osdakira
Keywords: hubot hubot-scripts
https://npm.im/hubot-github-pull-request-creator

hubot-github-name-slack-id-maphubot-github-name-slack-id-map
Keep correspondence between github_name and slack_idKeep correspondence between github_name and slack_id
Version 0.2.1 published 2018-06-04 by osdakira
Maintainers: osdakira
Keywords: hubot hubot-scripts
https://npm.im/hubot-github-name-slack-id-map

hubot-meme-generatorhubot-meme-generator
A modified version of the hubot script meme generator: https://github.com/github/hubot-scripts/blob/master/src/scripts/meme_generator.coffeeA modified version of the hubot script meme generator: https://github.com/github/hubot-scripts/blob/master/src/scripts/meme_generator.coffee
Version 1.1.7 published 2016-03-07 by jgrosecl49
Maintainers: benderthecrime
Keywords: hubot hubot-scripts
https://npm.im/hubot-meme-generator

hubot-github-hipchathubot-github-hipchat
A Hubot-on-hipchat Script for GitHubA Hubot-on-hipchat Script for GitHub
Version 0.1.5 published 2017-11-30 by terraflubb
Maintainers: terraflubb
Keywords: hubot hubot-scripts
https://npm.im/hubot-github-hipchat

hubot-github-managementhubot-github-management
A Hubot script for managing your GitHub organization's repositories and teams.A Hubot script for managing your GitHub organization's repositories and teams.
Version 2.0.0 published 2017-04-19 by contolini
Maintainers: contolini
Keywords: hubot hubot-scripts catops github
https://npm.im/hubot-github-management

hubot-slack-github-issue-linkhubot-slack-github-issue-link
A hubot script that posts detailed slack attachments for mentioned github issues and pull requestsA hubot script that posts detailed slack attachments for mentioned github issues and pull requests
Version 0.0.3 published 2016-04-25 by martinemde
Maintainers: martinemde
Keywords: hubot hubot-scripts github pull-requests issues github-issues github-pull-requests
https://npm.im/hubot-slack-github-issue-link

hubot-jenkins-gofhubot-jenkins-gof
Jenkins support for GitHub Organization Folder PluginJenkins support for GitHub Organization Folder Plugin
Version 0.0.1 published 2016-09-16 by gabealmer
Maintainers: gabealmer
Keywords: hubot hubot-scripts
https://npm.im/hubot-jenkins-gof

hubot-ghehubot-ghe
Interact with GitHub Enterprise through HubotInteract with GitHub Enterprise through Hubot
Version 0.1.2 published 2015-10-17 by mgriffin
Maintainers: mgriffin
Keywords: hubot hubot-scripts
https://npm.im/hubot-ghe

hubot-lgtmhubot-lgtm
Automatically merge pull requests after contributors have given the thumbs upAutomatically merge pull requests after contributors have given the thumbs up
Version 2.1.0 published 2018-02-11 by contolini
Maintainers: contolini
Keywords: hubot hubot-scripts github
https://npm.im/hubot-lgtm

hubot-github-mergehubot-github-merge
Merge branches on GitHub with HubotMerge branches on GitHub with Hubot
Version 1.0.0 published 2015-06-07 by maletor
Maintainers: maletor
Keywords: hubot hubot-scripts github merge githubot
https://npm.im/hubot-github-merge

hubot-github-merge-generichubot-github-merge-generic
Merge branches on GitHub with HubotMerge branches on GitHub with Hubot
Version 1.0.4 published 2016-07-11 by ggarber
Maintainers: ggarber
Keywords: hubot hubot-scripts github merge githubot
https://npm.im/hubot-github-merge-generic

hubot-octocathubot-octocat
A hubot script that provides Github repository informationA hubot script that provides Github repository information
Version 0.2.0 published 2016-09-02 by brianberg
Maintainers: brianberg
Keywords: hubot hubot-scripts github
https://npm.im/hubot-octocat

npm install <package-name>でインストールできます。

external-scripts.jsonというファイルを作成し、package名を列挙することで利用することができます。

[
   "hubot-diagnostics", 
   "hubot-help"
]

自前の処理を作成する

./scriptsというディレクトリを作成しそこへ自分で作成したスクリプトを配置することで、独自の処理を記述することができます。

書き方:Scripting

% mkdir scripts

.js, .mjsのどっちでも利用可能です。

会話を聞いてアクションする

これらは最低限の利用方法です。その他はドキュメント Scripting を参照ください。

./scripts/hello.mjs
"use strict";

export default async function(robot) {
  robot.hear(/誰かいますか/i, async res => {
    res.send(`私がいます`);
  });

  robot.respond(/こんにちは/i, async res => {
    res.reply(`hello!!`);
  });

  robot.hear(/さようなら/i, async res => {
    res.emote('またね');
  });
}
  • 発言への反応

    • hearはウォッチしているroomで発言があればすべて反応します。
    • respondはメンションやダイレクトなチャットなどbotに対しての発言に反応します。
  • 発言

    • sendはそのルーム全員への発言
    • replyは発言した者への発言(メンションになるはず?)
    • emoteは昔はなかった気がしますが、ルームに対する発言になるそう(sendとの違いは?)

起動して発言してみます。./scripts配下に設定したスクリプトは自動的に読み込まれ有効化されます。

% npx hubot --name sam
sam> 誰かいますか
私がいます
sam> こんにちは
Shell: hello!!
sam> さようなら
* またね
sam> exit

adapterについて

Adapters

--adapterというオプションを何も指定せずに起動した場合、hubotはデフォルトでshellというアダプターで起動します。
これはローカルのシェル上で対話するモードとなります。

% npx hubot --help
Usage: hubot [options]
  -a, --adapter HUBOT_ADAPTER
  -f, --file HUBOT_FILE
  -c, --create HUBOT_CREATE
  -d, --disable-httpd HUBOT_HTTPD
  -h, --help
  -l, --alias HUBOT_ALIAS
  -n, --name HUBOT_NAME
  -r, --require PATH
  -t, --config-check
  -v, --version

例えばSlackに接続する場合は、アダプターをインストールし、必要な環境変数をセットした上で--adapter@hubot-friends/hubot-slackを指定します。

% npm i @hubot-friends/hubot-slack
% export HUBOT_SLACK_APP_TOKEN=xapp-1-...
% export HUBOT_SLACK_BOT_TOKEN=xoxb-...
% npx hubot --name sam --adapter @hubot-friends/hubot-slack

最後に

scriptsexternal-scripts.jsonなど昔からのhubotの構成はそのままに、すべてECMAScript,ESMなjsで記述可能になっていることが確認できました。

アダプターも@hubot-friendsというprefixがつくようになったりしていて色々変わっているようですが、2017年当時と使用感は変わりませんでした。

Slack Boltがある中でどれだけ需要があるのかわかりませんが、
Teams用のアダプターがあるのはよさそうです。

https://github.com/hubot-friends/hubot-ms-teams

あとRocket.Chatも。

https://github.com/RocketChat/hubot-rocketchat

Coffee Scriptのまま放置しているチャットボットをそろそろメンテナンスしてあげようかなと思います。

Discussion