Telegram bots

13
Telegram Bots

Transcript of Telegram bots

Telegram Bots

WHOAMI

Front End Lead @ Rails Reactor github.com/viattik twitter.com/vi_attik

bots, bots, bots

И чо?

Creating Bot

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"); });

Creating Botfunction 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" }); });

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(); });

Support BotwsServer.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); }); });

DEMOhttps://youtu.be/1xhgMeV4ZCs

Inline Bots

Links

https://core.telegram.org/botshttps://telegram.me/botfatherhttps://github.com/viattik/support-demo-bothttps://medium.com/codeday-by-studentrnd/intro-to-node-js-making-a-telegram-bot-964b8cfe1129