Guest User

Chat Bot Node.js

a guest
Jan 3rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. const TelegramBot = require('node-telegram-bot-api');
  2.  
  3. // replace the value below with the Telegram token you receive from @BotFather
  4. const token = '398047618:AAFAWYC1a3lutjunglWL6IPcTMk1YXjxxxx';
  5.  
  6. // Create a bot that uses 'polling' to fetch new updates
  7. const bot = new TelegramBot(token, {polling: true});
  8.  
  9. // Matches "/echo [whatever]"
  10. bot.onText(/\/echo (.+)/, (msg, match) => {
  11. // 'msg' is the received Message from Telegram
  12. // 'match' is the result of executing the regexp above on the text content
  13. // of the message
  14.  
  15. const chatId = msg.chat.id;
  16. const resp = match[1]; // the captured "whatever"
  17.  
  18. // send back the matched "whatever" to the chat
  19. bot.sendMessage(chatId, resp);
  20. });
  21.  
  22.  
  23. var mysql = require('mysql');
  24. var con = mysql.createConnection({
  25. host: "localhost",
  26. user: "root",
  27. password: "",
  28. database: "node_js_chat_bot_telegram"
  29. });
  30.  
  31. con.connect();
  32.  
  33. var queryString = con.query("SELECT * FROM question WHERE name_question = 'shallom'");
  34. con.query(queryString, function(err, rows, fields) {
  35. if (err) throw err;
  36.  
  37. for (var i in rows) {
  38. console.log('Post Titles: ', rows[i].name_question);
  39. }
  40. });
  41.  
  42. // console.log(queryString);
  43. result = queryString;
  44.  
  45. con.end();
  46.  
  47. // result = queryString;
  48.  
  49. chat_1 = "shallom";
  50.  
  51. // Listen for any kind of message. There are different kinds of
  52. // messages.
  53. //
  54. bot.on('message', (msg) => {
  55.  
  56. var hi = "hi";
  57. if (msg.text.toString().toLowerCase().indexOf(hi) === 0) {
  58. // bot.sendMessage(msg.chat.id,"Hello dear user");
  59. bot.sendMessage(msg.chat.id,"Hello dear " + msg.from.first_name);
  60. }
  61.  
  62. var shallom = chat_1;
  63. if (msg.text.toString().toLowerCase().indexOf(shallom) === 0) {
  64. // bot.sendMessage(msg.chat.id,"Hello dear user");
  65. bot.sendMessage(msg.chat.id,"Shallom " + msg.from.first_name);
  66. }
  67.  
  68. var Var_Chat = chat_1;
  69. if (msg.text.toString().toLowerCase().indexOf(Var_Chat) === 0) {
  70. // bot.sendMessage(msg.chat.id,"Hello dear user");
  71. bot.sendMessage(msg.chat.id,result + msg.from.first_name);
  72. }
  73. //
  74. // var Var_Chat2 = con.query("SELECT * FROM question WHERE name_question = 'how are you'");
  75. // // var Var_Chat2 = 'SELECT * FROM question WHERE name_question = 'chat_1;
  76. // if (msg.text.toString().toLowerCase().indexOf(Var_Chat2) === 0) {
  77. // // con.query(queryString, function(err, rows, fields) {
  78. // // if (err) throw err;
  79.  
  80. // // for (var i in rows) {
  81. // // console.log('Post Titles: ', rows[i].name_question);
  82. // // }
  83. // // });
  84. // bot.sendMessage(msg.chat.id,result + msg.from.first_name);
  85. // }
  86.  
  87. var bye = "bye";
  88. if (msg.text.toString().toLowerCase().includes(bye)) {
  89. // bot.sendMessage(msg.chat.id, "Hope to see you around again , Bye");
  90. bot.sendMessage(msg.chat.id, "Have a nice day " + msg.from.first_name);
  91. }
  92.  
  93. var location = "location";
  94. if (msg.text.indexOf(location) === 0) {
  95. bot.sendLocation(msg.chat.id,44.97108, -104.27719);
  96. bot.sendMessage(msg.chat.id, "Here is the point");
  97.  
  98. }
  99.  
  100. var what = "idiot";
  101. if (msg.text.includes(what)) {
  102. bot.kickChatMember(msg.chat.id, msg.from.id);
  103. }
  104.  
  105. });
Add Comment
Please, Sign In to add comment