Updating Telegram Bot Commands In Realtime - python

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.

Related

Update preexisting telegram menus using python

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.

How to handle messages in a Telegram Bot?

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

Is there a way to create a invite to only be used by a specific user? - Disnake

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

Create a command to update a discord bot database

I have made a discord bot in python which makes use of databases to store channel ids where my bot is running. I have a command which allows me to add/remove items from this database, however, it will not actually update the database unless if I reboot the bot. For reference, this bot is written in discord.py and is written on repl.it. This is all I have code-wise, so far:
db['channel_ids'] = channel_ids #just to show which database I want to reference
#client.command(name = 'update_database')
async def update_database():
# want this function to simply make the database update/reload, if this is at all possible.
To add more information, I have a command which allows me to see all of the channel id's which my bot is running in. However, as mentioned, the list it returns may be outdated if I have added/removed some channel ids and not yet rebooted the bot. All I want is a way to get the database to update without having to reboot the bot. Thanks all.

CallbackQueryHandler reply message instead of edit message - Python Telegram Bot

I'm developing a bot using python telegram bot
I would like to be able to send a reply message from a InlineKeyboardButton instead of having to edit the current message.
def callback(update, context):
# this works (but I don't want to edit the current message but sending a new one)
update.callback_query.edit_message_text("response")
# this unfortunately does not work
update.message.reply_text("response") # None has no attribute 'reply_text'
...
updater.dispatcher.add_handler(CallbackQueryHandler(self.callback))
Only one of the optional attributes of Update will be present at a time. If the update from pressing an inline button, then update.callback_query is present, but update.message is not. However, update.callback_query.message may be present, depending on whether or not the message with the inline button was sent by the bot itself or via inline mode. If it is present, you can also access it via the convenience property update.effective_message.
Dislaimer: I' currently the maintainer of python-telegram-bot

Categories