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.
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).
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
About: I am using Disnake in python (version 3.9.7)
Hello all,
I am trying to make a bot where bot a creates a server (since bots can only be in 10 guilds if you want to be able to create guilds), and then a discord user(me) then invites a main bot which has permission to make slash commands and contains all the moderation code.
I need to create a invite that can only be used by the user that run the command to the main server which the slash command is run in. I am currently using Disnake's disnake.ext.commands.bot rather than disnake.client
i'm developing a python telegram bot and i have a script that is always running (for receiving new command from telegram) and i want the bot to send messages when the user perform a action in a website.
example: the user start the bot, the bot send a link to perform an action in the website (like login to the user's account and connect the telegram id with the user id) and then send a confirmation message on telegram that all's good.
my problem is how i can tell the python script that the action in the browser is done? for now i'm constantly query a database but my solution is pretty dumb because if the user don't perform any action the query can go forever.
any suggestion how to do it correctly?
thx <3
I see two viable solutions here:
Send the message directly from the website. While only one process is allowed to fetch updates at a time, you can make other requests from as many servers as you like. Depending on how your website works, you can make a plain HTTP request to the Bot API or use an API wrapper like python-telegram-bot or a wrapper in a different language to make the request. e.g. if you're running a php-based website, you could use a php API wrapper.
If for some reason 1. is not an option for you, you can try to inform your running process about the user login. the PTB FAQ have an entry that should help you get started. If your website & bot are running on the same server, it might be possible to make the update_queue directly available to the website process. If not, you can try to set up a webhook for your bot and post an update to the webhook that you then enqueue into the update_queue
Approach 1. has the downside that you don't have all the bot logic in one place, but it should be by far easier to implement than 2.
Disclaimer: I'm currently the maintainer of python-telegram-bot
Telegram allows the commands to be updated using setMyCommands. I can successfully update the commands in realtime based on the user input using a python API, pyTelegramBotAPI.
However, the problem is the user has to exit the chat with the bot and then come back to the chat again to see the new commands (by typing /).
Is there any way that I can have the bot update the list of commands in realtime with the user still in the chat?
Actually it is possible to do this real time if you set the scope to chat with specific user. There is parameter scope in set_my_commands function. By default set_my_commands affects all the chats of your bot. However, you can provide telegram.BotCommandScopeChat as a value of scope param. See the code below, it updates the command menu immediately without need to switch chats.
def _set_menu(commands: List[BotCommand], update: Update):
''' sets the chat commands menu '''
bot = Bot(os.getenv('TELEGRAM_TOKEN'))
bot.delete_my_commands()
if update:
bot.set_my_commands(
commands=commands,
scope=telegram.BotCommandScopeChat(
chat_id=update.effective_chat.id
))
else:
bot.set_my_commands(commands=commands)
So I tried it with telegram client (on linux and on android) and the commands do not change unless the user re-enters the chat. This I think is because the telegram-client only loads the commands when the user enters the chat.
But I also tried this with telegram web and found that the commands changed immidiately after I changed the command sets from BotFather. The webapp actually did load the command set without me leaving the chat, after sending a single message/command
So its definitely a problem with the telegram-client.