{"id":180,"date":"2015-06-23T14:25:58","date_gmt":"2015-06-23T06:25:58","guid":{"rendered":"http:\/\/vinta.ws\/code\/?p=180"},"modified":"2026-02-18T01:20:36","modified_gmt":"2026-02-17T17:20:36","slug":"setup-hubot-with-slack","status":"publish","type":"post","link":"https:\/\/vinta.ws\/code\/setup-hubot-with-slack.html","title":{"rendered":"Slack: Build a chat bot with Hubot in JavaScript"},"content":{"rendered":"<p>Hubot is an scriptable chat bot framework created by GitHub. The newer version supports JavaScript (ES6+), no more CoffeeScript!<\/p>\n<p>ref:<br \/>\n<a href=\"https:\/\/hubot.github.com\/\">https:\/\/hubot.github.com\/<\/a><br \/>\n<a href=\"https:\/\/slack.dev\/hubot-slack\/\">https:\/\/slack.dev\/hubot-slack\/<\/a><\/p>\n<h2>Installation<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-bash\">$ npm install -g yo generator-hubot\n\n$ mkdir codetengu-bot\n$ cd codetengu-bot\n$ yo hubot --adapter=slack<\/code><\/pre>\n<p>You could find all available adapters here:<br \/>\n<a href=\"https:\/\/hubot.github.com\/docs\/adapters\/\">https:\/\/hubot.github.com\/docs\/adapters\/<\/a><\/p>\n<h2>Slack Token<\/h2>\n<p>The next thing you need is a Slack Bot Token (API Token) which looks like <code>xoxb-xxx<\/code> for your chat bot app. You could create a Hubot app in your Slack workspace to request a token, for instance <a href=\"https:\/\/vintachen.slack.com\/apps\/A0F7XDU93-hubot\">https:\/\/vintachen.slack.com\/apps\/A0F7XDU93-hubot<\/a>.<\/p>\n<p>Otherwise, you could also create an universal Slack app, install it to your workspace. In your app settings, under &quot;Install App&quot; section, you are able to find <code>OAuth Tokens<\/code> for your chat bot. See <a href=\"https:\/\/api.slack.com\/apps\">https:\/\/api.slack.com\/apps<\/a>.<\/p>\n<p>ref:<br \/>\n<a href=\"https:\/\/api.slack.com\/bot-users\">https:\/\/api.slack.com\/bot-users<\/a><\/p>\n<h2>Development<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-console\">$ HUBOT_SLACK_TOKEN=YOUR_SLACK_BOT_TOKEN \n.\/bin\/hubot --adapter slack<\/code><\/pre>\n<p>I fork a script named <code>hubot-reload-scripts<\/code> to help you reload your scripts when developing them.<br \/>\n<a href=\"https:\/\/github.com\/vinta\/hubot-reload-scripts\">https:\/\/github.com\/vinta\/hubot-reload-scripts<\/a><\/p>\n<h2>Hear And Respond Messages<\/h2>\n<p>Writing your own script<br \/>\n<a href=\"https:\/\/hubot.github.com\/docs\/scripting\/\">https:\/\/hubot.github.com\/docs\/scripting\/<\/a><\/p>\n<pre class=\"line-numbers\"><code class=\"language-js\">\/\/ scripts\/your_script.js\n\/\/ Description\n\/\/   Do your shit\n\/\/\n\/\/ Author:\n\/\/   Vinta Chen\n\/\/\n\/\/ Commands:\n\/\/   * <code>restart &lt;service&gt;<\/code>* - Restart the service\n\/\/\nconst _ = require('lodash');\n\nmodule.exports = (robot) =&gt; {\n  robot.hear(\/restart ([a-z0-9_-]+)\/i, (res) =&gt; {\n    robot.logger.debug(<code>Received message: ${res.message.rawText}<\/code>);\n    const [ serviceName ] = res.match.slice(1);\n    res.send(<code>Restarting ${serviceName}<\/code>);\n    doYourShit();\n  });\n};<\/code><\/pre>\n<h2>Call Slack APIs<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-js\">robot.slack.channels.info({'channel': res.message.rawMessage.channel})\n  .then(apiRes =&gt; {\n    const purpose = apiRes.channel.purpose.value;\n    const topic = apiRes.channel.topic.value;\n    res.send(<code>purpose: ${purpose}<\/code>);\n    res.send(<code>topic: ${topic<\/code>);\n  })\n  .catch(apiErr =&gt; {\n    robot.logger.error('apiErr', apiErr);\n  });<\/code><\/pre>\n<p>ref:<br \/>\n<a href=\"https:\/\/slack.dev\/hubot-slack\/basic_usage#using-the-slack-web-api\">https:\/\/slack.dev\/hubot-slack\/basic_usage#using-the-slack-web-api<\/a><br \/>\n<a href=\"https:\/\/api.slack.com\/methods\">https:\/\/api.slack.com\/methods<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hubot is an scriptable chat bot framework created by GitHub. The newer version supports JavaScript (ES6+), no more CoffeeScript!<\/p>\n","protected":false},"author":1,"featured_media":753,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,7],"tags":[127,11],"class_list":["post-180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-about-devops","category-about-javascript","tag-chat-bot","tag-javascript"],"_links":{"self":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":0,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media\/753"}],"wp:attachment":[{"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinta.ws\/code\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}