Upload
others
View
33
Download
0
Embed Size (px)
Citation preview
Create a Telegram BOT
Bignotti Alberto
Telegram
Telegram is a cloud-based instant messaging and broadcasting service.
The official clients are open and free software. The server-side source code has not been made public.
Features of Telegram are the ability to exchange text messages between two users or between groups, make voice calls, exchange voice messages, video messages, photos, videos, stickers and files of any type up to 1.5 GB.
https://telegram.org/apps
Telegram
The messages sent are saved on the Telegram cloud. The apps allows the user to be able to access messages from several devices at the same time.User to UserGroupsTelegram groups can contain up to 200,000 membersChannelsThey are chats in which anyone who is an administrator can send messages to members of the channel, subscribers cannot reply or comment. A channel can contain an unlimited number of subscribers and can be public or privateBotBots are Telegram accounts, managed by a program, which offer multiple features with immediate and fully automated responses
Telegram BOT, use cases
• Notify events (via messages) to a user / group• Example: https://plugins.jenkins.io/telegram-notifications/
• Communicate, by sending and receiving commands/messages with a user / group• Example: https://youtu.be/uwlThnyeh9s?t=82
CLI++
Requirements
• Basic knowledge of HTTP(s) protocol and web API
• Json parser
• Http debugging proxy (aka HTTP sniffer)
• Http client
• Http server
HTTP(s) protocol and web API
Hypertext Transfer Protocol
• HyperText Transfer Protocol (HTTP) is an application layer protocol used as the main system for transmitting information on the web or in a typical client-server architecture.
• An HTTP server typically listens for client requests on port 80 (443 for https) using the transport layer TCP.
Json
• stands for JavaScript Object Notation
• is a lightweight format for storing and transporting data
• is often used when data is sent from a server to a web page
• is "self-describing" and easy to understand
https://www.json.org/json-it.html
Json
Http debugging proxy
https://www.telerik.com/fiddler
Fiddler is well known for its ability to capture HTTP(S) traffic, which makes it possible to debug any website or application. However, there is another feature that may be just as useful, and that’s creating HTTP(S) requests.
Create a Telegram bot
• Start a chat with BotFather
• Send command /newbot
• Follow instructions on wizard
At the time of creation, each BOT is given a unique authentication code, for example: 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
• To review and modify the bot configuration
/mybots
Telegram Bot API
https://core.telegram.org/bots/api
Queries to the Telegram Bot API must be in HTTPS and appear in this form:
https://api.telegram.org/bot<token>/METHOD_NAME
All methods in the Bot API are case-insensitive. We support GET and POST HTTP methods. Use either URL query string or application/json or application/x-www-form-urlencoded or multipart/form-data for passing parameters in Bot API requests.
Telegram Bot API
There are two ways to receive messages from Telegram
• Call HTTPS API getUpdates periodically
• Webhooks
Webhook (from the telegram server perspective)
Whenever there is an update for the bot, we will send an HTTPS POST request to the specified URL, containing a JSON-serialized Update.
Http server
• https://github.com/civetweb/civetweb
• Project mission is to provide easy to use, powerful, C (C/C++) embeddable web server.
• MIT license
• CivetWeb can be used by developers as a library, to add web server functionality to an existing application.
BOT
https://github.com/albertino80/tgm-server
• mkdir tgm
• cd tgm
• git clone [email protected]:civetweb/civetweb.git
• git clone [email protected]:albertino80/tgm-server.git
• cd tgm-server
• open tgm-server.pro with Qt Creator
Getting updatesThere are two mutually exclusive ways of receiving updates for your bot
• the getUpdates method (polling mode)
• and Webhooks
Incoming updates are stored on the server until the bot receives them either way, but they will not be kept longer than 24 hours.
https://core.telegram.org/bots/api#getupdates
getUpdates reply
{
"ok": true,
"result": [ {
"update_id": 322757181,
"message": {
"message_id": 414,
"from": {"first_name": "Albertino","language_code": "it"},
"chat": {"id": 965322014, "type": "private"},
"date": 1581547026,
"text": "Hello my friend"
}
}
]
}
Update OBJECT
getUpdates parameters
More…
Polling mode
While(true) {updateList = recv(URI/getUpdates?offset=maxUpdateId);maxUpdateId = findMaxId(updateList);forEach(update in updateList) {
reply = parseUpdate(update);if( !reply.isEmpty() )
send(reply);}maxUpdateId++;
}
Update OBJECT
Send a reply message
https://core.telegram.org/bots/api#sendmessage
More…
Webhook mode
httpServer.start
httpServer.on(POST, /telegramNewUpdate) {
update = http.payload
reply = parseUpdate(update);
if( !reply.isEmpty() ) {
send(reply.httpPath, reply.jsonDoc);
}
}
Update OBJECT
incoming Update JSON objectPollig or Webhook is the same
parseUpdate( JSON )
reply = Decode Update
reply = sendMessage
reply = answerInlineQuery
reply = …
replyOnChat(reply, chat_id)
Is inline query?
Switch Polling mode / Webhook
There are two Telegram API:
• setWebhook Use this method to specify a url and receive incoming updates via an outgoing webhook.
• deleteWebhook Use this method to remove webhook integration if you decide to switch back to getUpdates.
Switch Polling mode / Webhook
• https://api.telegram.org/bot%TOKEN%/setWebhook?url=%URL%
• https://api.telegram.org/bot%TOKEN%/deleteWebhook
• example/delete_web_hook.bat
• example/set_web_hook_albertino.bat
• Test: send_with_curl.bat
BOT commands
Commands present a more flexible way to communicate with your bot. The following syntax may be used:
/command
Deep linking
Allows for passing additional parameters to the bot on startup.
Each bot has a link that opens a conversation with it in Telegram —https://t.me/<bot username>. You can add the parameters start to this link, with values up to 64 characters long. For example:
https://t.me/bignodev_bot?start=test
Utente: Route0
Utente: Route1
Utente: Route2
Inline Bots https://core.telegram.org/bots/inline
Keyboards https://core.telegram.org/bots#keyboards
Inline keyboards https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating
API sendpoll https://core.telegram.org/bots/api#sendpoll
40
Bignotti Alberto
Senior Software Architect
albertino - italiancpp.slack.com
@albertino80