Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Telegram bots

592 views

Published on

Lightning talk about creating Telegram Bot

Published in: Software
  • Be the first to comment

Telegram bots

  1. 1. Telegram Bots
  2. 2. WHOAMI Front End Lead @ Rails Reactor github.com/viattik twitter.com/vi_attik
  3. 3. bots, bots, bots
  4. 4. И чо?
  5. 5. Creating Bot
  6. 6. Creating Bot var TelegramBot = require('node-telegram-bot-api'),
 telegram = new TelegramBot("YOUR_BOT_TOKEN", { polling: true });
 
 telegram.on("text", (message) => {
 telegram.sendMessage(message.chat.id, "Hello world");
 });
  7. 7. Creating Bot function getReply(text) {
 if (text.includes('ничо')) {
 return 'Вот и хорошо';
 } else if (text.includes('/start')) {
 return 'Давай, расскажи мне о своих проблемах.';
 } else {
 return 'И чо?';
 }
 }
 
 telegram.on("text", (message) => {
 const text = message.text.toLowerCase();
 const reply = getReply(text);
 telegram.sendMessage(message.chat.id, reply, {
 parse_mode: "Markdown"
 });
 });
  8. 8. Support Bot // Telegram sent message telegram.on("text", (message) => {
 const text = message.text;
 const id = message.chat.id;
 let chat = chats[id];
 if (!chat && text === '/support') {
 chats[id] = chat = {
 from: message.from,
 messages: []
 }
 } else if (chat && text === '/end_support') {
 chats[id] = chat = undefined;
 }
 if(chat) {
 chat.messages.push({ sender: 0, text });
 }
 syncToBrowser();
 });
  9. 9. Support Bot wsServer.on('request', function(request) {
 let connection = request.accept(null, request.origin);
 connections.push(connection);
 connection.sendUTF(JSON.stringify(chats));
 
 // Browser sent message
 connection.on('message', function(message) {
 const { id, text } = JSON.parse(message.utf8Data);
 const chat = chats[id];
 if (!chat) { return; }
 
 chat.messages.push({ sender: 1, text });
 telegram.sendMessage(id, text);
 });
 });
  10. 10. DEMO https://youtu.be/1xhgMeV4ZCs
  11. 11. Inline Bots
  12. 12. Links https://core.telegram.org/bots https://telegram.me/botfather https://github.com/viattik/support-demo-bot https://medium.com/codeday-by-studentrnd/intro-to-node-js- making-a-telegram-bot-964b8cfe1129

×