MUstar

Node.js - Slack Bot Example with Database

Aug 6th, 2018
250
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Connect Database;
  2. let mysql = require('mysql');
  3. let conn  = mysql.createConnection({
  4.     host: "DBHOST",
  5.     user: "DBUSER",
  6.     password: "DBPASSWD",
  7.     database: "DBNAME"
  8. });
  9.  
  10. let Botkit = require('botkit');
  11. let controller = Botkit.slackbot();
  12. let bot = controller.spawn({
  13.     token: "<Slack API Token>"
  14. });
  15.  
  16. bot.startRTM(function(err,bot,payload){
  17.     if(err){
  18.         ShowError("Cloud not Connection to Slack",err);
  19.     }
  20. });
  21.  
  22. conn.connect(function (err) {
  23.     if(err){
  24.         ShowError("Not Connected Database! ",err);
  25.     }
  26.     console.log("Connected Database");
  27.  
  28.     conn.query ("SELECT * FROM Command_list",function (err,result) {
  29.         if(err){
  30.             ShowError("Database Query Error",err);
  31.         }
  32.         result.forEach(function (data) {
  33.             controller.hears(data.question.split(','),["direct_message","direct_mention","mention","ambient"],function(bot,message){
  34.                 bot.reply(message,data.answer);
  35.             })
  36.         });
  37.         controller.hears([".*" ],["direct_message","direct_mention","mention","ambient"],function(bot,message){
  38.             bot.reply(message,'죄송합니다. 아직 등록된 답변이 없습니다.\n개발지원실에 답변등록요청 해주세요.');
  39.         });
  40.     });
  41. });
  42.  
  43. function ShowError(command,err){
  44.     throw new Error(`${command} \nSee Error: ${err}`);
  45. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×