Home python Appeal to Telegram Botton Boton

Appeal to Telegram Botton Boton

Author

Date

Category

There is a telegram bot. Task Write a script that can send a query bot and receive answers. It will be enough for the script to work without a telegram installed on a computer. Language – Python 3. Please tell me if it is possible to use which libraries. (Using in the user login / passarol script can not be


Answer 1, Authority 100%

In truth, it is not clear why this approach is needed by the development of a bot, and rather a working decision cannot be created, but nevertheless, it is possible to realize something like that, but not as much as you have thought.

In fact, it is possible to realize something about what you want a crutch (I would even call the crutch) – create a public group, configure the batter rights, get the supergroup identifier, and remove all users from the group, then Yes – in this case, there really do not need a single account in Telegram to send a message somewhere, but in fact there is no client to interact with the bot, and the question arises – why do you need a monologue? Therefore, the correct and only solution is needed at least one account, or rather Chat_id (Login and password have not been transmitted anywhere and not used, only chat identifiers).

API Telegram, like everyone else is a client-server solution. That is, the client is poisoning something to the server, after that the server gives the result and then the information is given to the client. Therefore, it can be assumed that without a client, such a task is in principle impossible.

Let’s look into the official documentation elementary to the simplest method to send simple messages SendMessage . You can see that the user ID (i.e. the client) is mandatory when sending a message

Based on this, you need at least one account in Telegram-E to interact with the bot. If you need to send a group / channel – negative identifiers are used, for example -10011234567890 but! As you need to know, the Bot management occurs through the API, that is, it is used by JSON -Pastes. Based on this, it is not necessary to use ready-made libraries to develop bots, in fact it is wrappers on Telegram methods, all of them are built on the same principle that I described above – sending to the data server by the client and their receipt. As they say – they have already done everything for us, simplifying our life, simple mortal :). But if you are strong in spirit, the obstacles are not terrible to you, and you want to go heavy, that is, describe all the methods desired for you (there is no special sense for you, because they have already written for us) Therefore, we will turn on only some methods for implementing an example.

bots can work 2 in different ways – one through getupdates , and the second via Webhook . You can google about LONG_POLLING and Webhook for bots, as it will be available on the first link in Google, I will not go into it, I will take it for simplicity Getupdates .

To send to Chat / Group Message, you need to send Post Code to the Telegram server. We implement it through an easy to use Requests

Simple example of sending a message:

import requests
token = 'your token'
Data = {'Chat_id': 'Your ID', 'TEXT': 'Test Message'}
R = Requests.post (F'Https: //api.telegram.org/bot {token} / SendMessage? ', Data = DATA)
Print (R.JSON ())
# and further your interaction with `json` object

A simple example of receiving the message that the user sent a bot:

import requests
token = 'your token'
# DATA_OFFSET = {'offset': 1234567801}
R = Requests.post (F'Https: //api.telegram.org/bot {token} / getupdates') #, Data = Data_Offset)
Print (R.JSON ())

data_offset is needed to confirm on the Telegram server that you answered this message. But that the answer turned out to be confirmed – you need to increment offset per unit more, that is, send data_offset = {'offset': 1234567802}

is in a nutshell, as it works. To implement it all – you will need not one, and even two days.

So the Council is to drive your task architecture. I have everything 🙂

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions