用hubot和slack定制chatops
适用环境:CentOS
Add Node.js Yum Repository
# yum install -y gcc-c++ make
# curl -sL https://rpm.nodesource.com/setup_6.x | sudo -E bash -
Install Node.js and NPM
# yum install nodejs
Install yo, hubot, coffee and generator
# npm install -g yo hubot coffee-script generator-hubot
Make directory
$ cd ~
$ mkdir mybot
$ cd mybot
Generate bot
$ yo hubot
? ==========================================================================
We're constantly looking for ways to make yo better!
May we anonymously report usage statistics to improve the tool over time?
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== Yes
_____________________________
/ \
//\ | Extracting input for |
////\ _____ | self-replication process |
//////\ /_____\ \ /
======= |[^_/\_]| /----------------------------
| | _|___@@__|__
+===+/ /// \_\
| |_\ /// HUBOT/\\
|___/\// / \\
\ / +---+
\____/ | |
| //| +===+
\// |xx|
? Owner yourname
? Bot name mybot
? Description mybot
? Bot adapter slack
create bin/hubot
create bin/hubot.cmd
create Procfile
create README.md
create external-scripts.json
create hubot-scripts.json
create .gitignore
create package.json
create scripts/example.coffee
create .editorconfig
_____________________________
_____ / \
\ \ | Self-replication process |
| | _____ | complete... |
|__\\| /_____\ \ Good luck with that. /
|//+ |[^_/\_]| /----------------------------
| | _|___@@__|__
+===+/ /// \_\
| |_\ /// HUBOT/\\
|___/\// / \\
\ / +---+
\____/ | |
| //| +===+
\// |xx|
Note: Bot adapter: slack
Remove unuse script
- Open file
external-scripts.jsonand delete row aboutheroku,redis - Remove file
hubot-scripts.json
Run it
$ ./bin/hubot
Test
mybot> mybot help
mybot> mybot ping
Press ctrl+c to quit.
Integration for slack
- Access
https://your-team.slack.com/apps/search?q=hubotand add hubot app - Copy token and save setting
Make startup script
$ vi mybot.sh
#!/bin/bash
export HUBOT_SLACK_TOKEN=xoxb-Your-token
./bin/hubot --adapter slack &
- Run
mybot.sh - Invite
mybotfrom your slack channel#general - Typing
mybot pingin your slack channel#general - May be
mybotresponsePONG.
Let hubot execute shell script
- install hubot-script-shellcmd
npm install hubot-script-shellcmd
cp -R node_modules/hubot-script-shellcmd/bash ./
-
Open file
external-scripts.jsonand add rowhubot-script-shellcmd -
Customize your shell in directory
bash/handlers,helloworldandupdateis sample. -
Run
mybot.shand try it.
$ sh mybot.sh
mybot> mybot shellcmd helloworld
Additional
- Alias from
shellcmdtorun- open file
mybot.shand add rowexport HUBOT_SHELLCMD_KEYWORD=run
- open file
- keep online
cd mybotandnpm install --save forevervi mybot/bin/hubot- modify line
exec node_modules/.bin/hubot --name "mybot" "$@"change toforever start -c coffee node_modules/.bin/hubot --name "mybot" "$@" - restart mybot