{"id":213,"date":"2018-09-06T13:52:23","date_gmt":"2018-09-06T12:52:23","guid":{"rendered":"http:\/\/brgeek.com.br\/wordpress\/?p=213"},"modified":"2018-09-06T13:52:23","modified_gmt":"2018-09-06T12:52:23","slug":"slack-bot-with-npm-node-js","status":"publish","type":"post","link":"http:\/\/brgeek.com.br\/wordpress\/2018\/09\/06\/slack-bot-with-npm-node-js\/","title":{"rendered":"Slack bot with NPM + Node.js"},"content":{"rendered":"<p>Hello fellow friends, just playing a bit with a Bot + slack decide to test locally with NPM + node.js which is just an easy go, it gets more complex when you build up the message structure as this is not an A.I we have to foresee the questions and responses.<\/p>\n<p>Lets get down to it<br \/>\n<strong>Installing Node and NPM<\/strong><\/p>\n<p>1) Visit the official Node.js website to get the installer.<br \/>\n2) After download, run the installer.<br \/>\n3) Restart your computer to ensure the changes can take effect.<\/p>\n<p><a href=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/node-installer.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/node-installer-300x219.png\" alt=\"\" width=\"429\" height=\"313\" class=\"alignnone wp-image-214\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/node-installer-300x219.png 300w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/node-installer.png 495w\" sizes=\"auto, (max-width: 429px) 100vw, 429px\" \/><\/a><\/p>\n<p>The Node.js installer should have also installed<span>\u00a0<\/span><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.npmjs.com\/get-npm\">NPM<\/a><span>\u00a0<\/span>for you. To confirm that you have installed both properly, you&#8217;ll need to open<span>\u00a0<\/span><strong>Windows Command Prompt<\/strong><span>\u00a0<\/span>if you&#8217;re on Windows, or<span>\u00a0<\/span><strong>Terminal<\/strong><span>\u00a0<\/span>if you&#8217;re on Mac or Linux.<\/p>\n<p>To check if you installed node:<\/p>\n<blockquote><p><strong>node -v<\/strong><\/p><\/blockquote>\n<p>To check if NPM is installed<\/p>\n<blockquote><p><strong>npm -v\u00a0<\/strong><\/p><\/blockquote>\n<p>If both of the commands return\u00a0results we are good to go.<\/p>\n<h2 id=\"initializing-our-project-and-installing-slackbotsjs\"><strong>Initializing our Project and Installing Slackbots.js<\/strong><\/h2>\n<p>Create a folder anywhere you&#8217;d like to serve as the root directory for your bot. Navigate to that folder and initialize a new project by running<\/p>\n<blockquote><p>npm init<\/p><\/blockquote>\n<p>Feel free to enter in whatever information you&#8217;d like. After that finishes running, you will be left with a file called.<span><\/span><code class=\"\" data-line=\"\">package.json<\/code><\/p>\n<p>Now, to simplify our interactions with Slack&#8217;s<span>\u00a0<\/span><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/api.slack.com\/rtm\">Real Time Messaging API<\/a>, we will be using the<span>\u00a0<\/span><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/mishk0\/slack-bot-api\">Slackbots.js library<\/a>. To install Slackbots.js for use in our project, run the installation command:<\/p>\n<blockquote><p><strong>npm install slackbots &#8211; -save<\/strong><\/p><\/blockquote>\n<p><span>We are now ready to use Slackbots.js in our code!<\/span><\/p>\n<h2 id=\"creating-a-new-slack-bot\"><strong>Creating a New Slack bot<\/strong><\/h2>\n<p>Let&#8217;s register a new bot on our slack instance. Head on over to:<code class=\"\" data-line=\"\"><br \/>\nhttps:\/\/yourinstancehere\/services\/new\/bot<\/code><\/p>\n<p>Replace with<span><\/span><code class=\"\" data-line=\"\">instance<\/code><span><\/span> the name of your Slack instance. Once here, give your bot a name. We&#8217;ll call our bot<span>\u00a0<\/span><strong>AD-BOT<\/strong>.<\/p>\n<p>After you hit,<code class=\"\" data-line=\"\">Add bot integration<\/code><span> you&#8217;ll be provided with your bot&#8217;s\u00a0<\/span><strong>API token<\/strong><span>. You&#8217;ll need this, later on, to save this somewhere. Additionally, you can also change the name of your bot, upload an icon, and set what channels your bot will operate inside.<\/span><\/p>\n<h2 id=\"building-our-bot\"><strong>Building our Bot<\/strong><\/h2>\n<p>In the same directory that your<span>\u00a0<\/span><code class=\"\" data-line=\"\">package.json<\/code><span>\u00a0<\/span>lives in, create a new file called.<span><\/span><code class=\"\" data-line=\"\">index.js<\/code> This file will serve as the code for your bot.<\/p>\n<p>This is my first code but you can adjust for your purposes<code class=\"\" data-line=\"\"><br \/>\n\/\/ create a bot<\/code><\/p>\n<p>var SlackBot = require(&#8220;slackbots&#8221;);<br \/>\nvar channel = &#8220;YourchannelHere&#8221;;<\/p>\n<p>var bot = new SlackBot({<br \/>\ntoken: &#8220;YOURTOKENHERE&#8221;,<br \/>\nname: &#8220;AD-BOT&#8221;<br \/>\n});<\/p>\n<p>bot.on(&#8220;start&#8221;, function() {<br \/>\nbot.postMessageToChannel(channel, &#8220;Welcome to Active Directory Channel&#8221;);<br \/>\nconsole.log(&#8220;BOT is now Live!&#8221;);<br \/>\n});<\/p>\n<p>bot.on(&#8220;message&#8221;, function(data) {<br \/>\nif (data.type !== &#8220;message&#8221;) {<br \/>\nreturn;<br \/>\n}<\/p>\n<p>handleMessage(data.text);<br \/>\n});<\/p>\n<p>function handleMessage(message) {<br \/>\nswitch(message) {<br \/>\ncase &#8220;hi&#8221;:<br \/>\ncase &#8220;hello&#8221;:<br \/>\nsendGreeting();<br \/>\nbreak;<br \/>\ndefault:<br \/>\nreturn;<br \/>\n}<br \/>\n}<\/p>\n<p>function sendGreeting() {<br \/>\nvar greeting = getGreeting();<br \/>\nbot.postMessageToChannel(channel, greeting);<br \/>\n}<\/p>\n<p>function getGreeting() {<br \/>\nvar greetings = [<br \/>\n&#8220;hello!&#8221;,<br \/>\n&#8220;hi there!&#8221;,<br \/>\n&#8220;Hi! Hope you are having fun today!&#8221;,<br \/>\n&#8220;how do you do!&#8221;,<br \/>\n&#8220;Howdy&#8221;<br \/>\n];<br \/>\nreturn greetings[Math.floor(Math.random() * greetings.length)];<br \/>\n}<\/p>\n<p><a href=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/code.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/code-300x159.png\" alt=\"\" width=\"630\" height=\"334\" class=\"wp-image-215 aligncenter\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/code-300x159.png 300w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/code-600x319.png 600w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/code-768x408.png 768w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/code-1024x544.png 1024w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/code.png 1912w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/p>\n<p>Our new code has our bot listen on the<span>\u00a0<\/span><code class=\"\" data-line=\"\">start<\/code><span>\u00a0<\/span>event which is fired when a connection to Slack is established. Once the event fires, it will run the provided function. In that function, we tell our bot to send the text<span>\u00a0<\/span><code class=\"\" data-line=\"\">Hello world!<\/code><span>\u00a0<\/span>to the channel you defined in the<span>\u00a0<\/span><code class=\"\" data-line=\"\">channel<\/code>variable. Finally, we threw in a<span>\u00a0<\/span><code class=\"\" data-line=\"\">console.log<\/code><span>\u00a0<\/span>just for good measure.<\/p>\n<p>Now it&#8217;s time to run our bot. To do so, simply run your<span>\u00a0<\/span><code class=\"\" data-line=\"\">index.js<\/code><span>\u00a0<\/span>file by running this command at the same directory:<\/p>\n<p><code class=\"\" data-line=\"\"><br \/>\n    node index.js<\/code><\/p>\n<p>If everything went well, not only should you see printed<span style=\"color: #222222; font-family: monospace;\"><span style=\"background-color: #e9ebec;\">\u00a0BOT is now Live!<\/span><\/span> on your console, but if you go to the designated channel on Slack, you should also see your bot saying <code class=\"\" data-line=\"\">Welcome to Active Directory Channel<\/code> in your channel, you can change that accordingly.<span><\/span><\/p>\n<p><a href=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/bot-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/bot-1-300x114.png\" alt=\"\" width=\"379\" height=\"144\" class=\" wp-image-218 aligncenter\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/bot-1-300x114.png 300w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/bot-1-600x227.png 600w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/bot-1-768x291.png 768w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/bot-1.png 1011w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/code.png\"><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello fellow friends, just playing a bit with a Bot + slack decide to test locally with NPM + node.js which is just an easy go, it gets more complex when you build up the message structure as this is not an A.I we have to foresee the questions and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":220,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[34,31,30,32,26,33],"class_list":["post-213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-api","tag-bot","tag-node-js","tag-npm","tag-slack","tag-slackbot"],"jetpack_featured_media_url":"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2018\/09\/slack-logo.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":2,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":219,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/213\/revisions\/219"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media\/220"}],"wp:attachment":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}