It has become fashionable to use messengers as a platform for the so-called "conversational commerce": now, in addition to smoothies and co-working, a self-respecting startup must acquire a bot or, at least, a channel in a telegram. To help them, we not only wrote a bot who was looking for the best coffee smoothies and co-workers and burgers in the city, but also an article about how we designed it.
In our small tutorial, we will talk about how to create a bot, set up a graphical menu, edit messages and send text and photos in one message.
Tagvisor - service for finding real reviews about the institution. We aggregate them by geolocation, hashtags, and from the official accounts of institutions in social networks. Next, we rank in popularity (number and freshness of posts on social networks) and proximity to the user.
Custdev showed that our target audience is often looking for places near the phone. Does not like to see more than 20 options in the search results, wants to immediately see reviews, photos and mark places on the map.
It would be logical to roll out a mobile application, but the development cost and entry threshold for a potential user is too high for a young startup. We decided to go the fashionable way and released a bot as an mvp.
@TagvisorBot shows the institution closest to you in the selected category.
As in the desktop version of the service, the main content of the issue are posts from social networks.
For implementation, Node.js and fork were chosen for the @ node-telegram-bot-api module, adapted for Telegram 2.0.
We mainly used the API telegram documentation and the node-telegram-bor-api module. For people who love Russian documentation, there is a translation , but it is still in the process. There is also a group of developers from all over Russia in Telegram itself) and on facebook . In groups you can always write a question, usually answer within a couple of days.
To create a bot, first of all, knock on @botfather with the / newbot command. He will ask you to write the name of the bot and its username (which should end with bot). When creating the bot, @botfather will report the http-token for api. Commands beginning with / set will help you to configure various parameters of the bot, such as name, description, avatar, text in profile and description. Everything is quite simple.
For the bot to work, you need to select the update mode: polling or WebHook.
To start the bot, you need to connect the package and transfer the necessary data to the bot: token and update mode.
const TelegramBot = require('./lib/telegram.js'); const bot = new TelegramBot('YOUR_TOKEN', {polling: true}); bot.getMe().then(function (me) { console.log('Hi my name is %s!', me.username); });
The call to the getMe () function occurs when the bot token is successfully authorized and the program is issued. In this case, the function getMe (). Then () takes the function function (me) as an argument. Its first argument is an object with which you can get various information about your bot and, in this case, output it to the console.
Next, to teach the bot to send messages, you need to use the bot.sendMessage method (chatId, 'Hello World !!!'); And for a bot to understand incoming requests, it is enough to use bot.on ('message', function (msg) {}). The methods are described in more detail in the node-telegram-bot-api documentation.
In order for the user to see the inline keyboard instead of the standard one, you need to send its parameters to the sendMessage method when sending messages:
const opt = { parse_mode: 'markdown', disable_web_page_preview: false, reply_markup: JSON.stringify({ inline_keyboard: [ [{text: ` ${emoji.get('ru')}`, callback_data:'rus'}, {text: `English ${emoji.get('gb')}`, callback_data:'eng'}] ] }) } bot.sendMessage(chatId, 'language?',opt);
Where text is the text on the button, callback_data is the data that the bot will receive when the button is pressed.
Accordingly, in order to catch the events of pressing a certain button, you need to use the method.
bot.on('callback_query', function (msg) { if (msg.data === 'rus'){ console.log(“Russian”); } if (msg.data === 'eng'){ console.log(“English”); } });
Sometimes you want not to send the message again, and correct the already existing (for convenience of displaying data). To do this, we decided to use the bot.editMessageText (text, opt) method; where text is the modified message text, opt - Additional options for requesting a telegram, such as the presence of a keyboard, text formatting, and more.
The standard sendPhoto method is not suitable, as it has limitations on the number of characters in the text and other formatting - the text is written under the photo. Therefore, we used sendMessage and simply inserted a link to the desired image into the message text.
In order for the telegram to load the image into the message, you need to set the disable_web_page_preview parameter: false.
var opt = { chat_id: chatId, parse_mode: 'markdown', disable_web_page_preview: false, reply_markup = JSON.stringify({ inline_keyboard: [..] }) }
To format the text you need to set the parse_mode parameter. It has two meanings: markdown and html. If the value is markdown, formatting is used:
*bold text* _italic text_ [text](URL) `inline fixed-width code` ```pre-formatted fixed-width code block``` const text = '[](url)'; bot.sendMessage(chatId, text);
To see all this beauty of the world, and not just your users, you can write an article on Habr upload your bot to the store store store bot.
Registration and publication is simple - just log in on the storebot.met website and then click “AddBot” in the menu “Add-Dele-Ready”. This is not a moderation in the AppStore.
So, with the help of our small tutorial, you could learn how to make a bot in a telegraph. We hope it was useful for you. And if you are a cool developer, and it was all obvious to you, then we invite you to participate in a small challenge: as we already wrote above, Tagvisor also has a desktop version, which we recently launched in test mode. And to the one who finds the most bugs (in the layout, code, mobile version), we will organize the delivery of three large pizzas. Found bugs send mail bugtracker@tagvisor.com.
Source: https://habr.com/ru/post/306036/
All Articles