I am writing a telegram bot in python.
One of my bot functionalities is to delete all of the messages in the chat. I find the function bot.delete_message however it delete only one message according to message id and I don't have the ids for every message.
I also saw in the telegram api some functions that can help me, like https://core.telegram.org/method/channels.deleteHistory or https://core.telegram.org/method/messages.getHistory (will allow me to get the id of every message and then delete them). but I don't see how I can call these functions in the python api.
Is there a way to call these functions with the python api? or perhaps is there another way to delete entire chat history?
Telegram Bot API doesn't have methods to delete multiple messages. You have to know message_id to delete it.
The methods which you've mentioned in the question are not part of Telegram Bot API, but they're part of Telegram API. python-telegram-bot library has support to only Bot API. You can make use of telethon library which supports Telegram API.
Related
What I'm trying to understand is how Discord.py creates and sends responses from their on_message function.
Take this as an example:
#client.event()
async def on_message(message):
print(message.content)
I'm trying to understand how Discord.py retrieves new messages from Discord without refreshing the channel histories for every single channel in every single server to scan for new messages which would surely hit Discord's rate limit.
Is there a way to scan for new messages with Discord's API using fetch or post requests? I am not trying get a solution on how to scan new messages using an already made library. I want to achieve this using only the requests module in python.
I'm trying to understand how Discord.py retrieves new messages from Discord without refreshing the channel histories for every single channel in every single server to scan for new messages which would surely hit Discord's rate limit.
Discord bots establish a websocket connection with Discord's servers, which is essentially a (secure) two-way tunnel. This means that once the connection is up, Discord can send events to you. Instead of you having to manually fetch every single channel, Discord tells you "Hey, a message was created", and the payload attached will give all the additional info about it.
The Discord (not discord.py) docs have detailed info about how everything works behind the scenes, to help the people creating the Bot libraries for you. For example, this section details which types of events Discord can send to you. To see how something is constructed, click on one of the event types and read up on the data that Discord provides.
Is there a way to scan for new messages with Discord's API using fetch or post requests? I am not trying get a solution on how to scan new messages using an already made library. I want to achieve this using only the requests module in python.
Not really, unless you do in fact send a GET for every single channel, which will get you ratelimited. There's really no reason to ever use only GET/POST requests (other than Webhooks, where you just send a POST with your info to send a message to a channel without a bot).
If you'd like to read up on Discord's API, the docs I linked contain a full spec of everything, so you could try to do whatever your heart desires (... and the API supports).
How is it possible to get all chats in the form of a iterabale object (list-like) or iterate over all possible chats the bot is an admin/participant of (using python-telegram-bot)? Is there a python-telegram-bot method for getting all chats a bot is a admin/participant of?
No, there is not, because the Telegram Bot API does not provide such a method. You can use the Update.my_chat_member updates to keep track of that. See here for an example on how that can be done using python-telgeram-bot.
Disclaimer: I'm currently the maintainer of python-telegram-bot.
I am building a telegram bot that will automatically add messages to preexisting menus in a bot ( created by Manybot, not with python ) once they are uploaded to a specific channel.
I spent a week or so trying to find a way to update preexisting menus but it can't manage to find a solution.
even when I try to run an infinite loop to monitor any messages to the bot ( using any telegram-python library out there ) i get this error :
Conflict: can't use getUpdates method while webhook is active; use deleteWebhook to delete the webhook first
and i am forced to delete the webhook , which will in turn disable every command i added to the menus and need to create another bot
I would really appreciate any help .
The problem is not related to existing webhooks, it's the fact that each bot token can poll / webhook only once.
In other words, you can't have two scripts running with the same bot token, regardless if it's long polling or a webhook.
This is a limitation of the Telegram Bot API.
You can only run your own script when the manybot version of your bot is paused, stopped, turned off, etc. The getUpdates method will reject new connections with the same bot token.
You can either use a second bot token to use other commands if your setup allows it, or use the MTProto API with frameworks such as Pyrogram or Telethon.
My goal is to make a Telegram Bot using Python that does the following:
The user types a command
The bot explains what the user should type next.
The user types certain information
I use that info to fetch a value in a python dictionary.
The bot replies with that info
I have already created the bot and set the command. The problem is that I don't know how to keep the bot "listening" . I have looked up in the docs but I have only found getUpdates, which gets the user's responses but only when you hit run.
res=requests.get(url=f"https://api.telegram.org/bot{bot_token}/getUpdates")
I would like to set a webhook, and I know there is a method for this but I'm not shure about how this works.
When you are a PHP programmer, setting the webhook through a URL and using setWebhook would do the trick and telegram will send the result to this link whenever a user sends an update to the bot. But, Python programmers have to use a different approach, I think. The main, and while, the easiest approach to make the bot listen permanently to the request is to python-telegram-bot module.
There are a few links that can help to build your first pythonic bot that can respond to users' updates.:
Python-Telegram-Bot Repository, which is the repository of the module.
A few examples of bots created using this module, can give you insight into the process of creating your first bot.
A conversation bot
I know how to get all the IDs from a chat via the received message (update.message.chat.id)
However I couldn't find a function in the docs of Pybot that allows to get a message with a specific ID from the chat (from the past).
Is there such a function available?
EDIT:
I found a function that should do what I want but it seems it is not available in Python-Telegram-Bot?
https://core.telegram.org/method/messages.getMessages#
According to the note, it should be useable by bots as well.
There is a difference between Telegram API and Telegram Bot API.
Link you provided is about Telegram API, and Bot API doesn't have method like that you want.