Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting...

40
Create a Telegram BOT Bignotti Alberto

Transcript of Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting...

Page 1: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Create a Telegram BOT

Bignotti Alberto

Page 2: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 3: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code
Page 4: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 5: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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++

Page 6: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Requirements

• Basic knowledge of HTTP(s) protocol and web API

• Json parser

• Http debugging proxy (aka HTTP sniffer)

• Http client

• Http server

Page 7: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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.

Page 8: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code
Page 9: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 10: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Json

Page 11: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Qt

+

https://www.qt.io/download

Page 12: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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.

Page 13: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Create a Telegram bot

https://core.telegram.org/bots

Page 14: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 15: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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.

Page 16: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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.

Page 17: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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.

Page 18: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code
Page 19: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code
Page 20: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 21: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 22: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 23: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

getUpdates parameters

More…

Page 24: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 25: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Send a reply message

https://core.telegram.org/bots/api#sendmessage

More…

Page 26: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Webhook mode

httpServer.start

httpServer.on(POST, /telegramNewUpdate) {

update = http.payload

reply = parseUpdate(update);

if( !reply.isEmpty() ) {

send(reply.httpPath, reply.jsonDoc);

}

}

Update OBJECT

Page 27: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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?

Page 28: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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.

Page 29: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 30: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

BOT commands

Commands present a more flexible way to communicate with your bot. The following syntax may be used:

/command

Page 31: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

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

Page 32: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Deep linking Example

http://127.0.0.1:8081/

./www/index.html

Page 33: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Utente: Route0

Page 34: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Utente: Route1

Page 35: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Utente: Route2

Page 36: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Inline Bots https://core.telegram.org/bots/inline

Page 37: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Keyboards https://core.telegram.org/bots#keyboards

Page 38: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

Inline keyboards https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating

Page 39: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

API sendpoll https://core.telegram.org/bots/api#sendpoll

Page 40: Create a Telegram BOT · Telegram Telegram is a cloud-based instant messaging and broadcasting service. The official clients are open and free software. The server-side source code

40

Bignotti Alberto

Senior Software Architect

[email protected]

albertino - italiancpp.slack.com

@albertino80