CallbackQueryHandler reply message instead of edit message - Python Telegram Bot - python

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

Related

ConversationHandler state triggers only if the message starts with '/' in groups chat [duplicate]

My telegram bot receives messages sent by user to my bot in private chats but not receives messages sent by users in group chats. Any options/api for getting group chat messages also,.
Talk to #botfather and disable the privacy mode.
Sequence within a BotFather chat:
You: /setprivacy
BotFather: Choose a bot to change group messages settings.
You: #your_name_bot
BotFather: 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username.
'Disable' - your bot will receive all messages that people send to groups.
Current status is: ENABLED
You: Disable
BotFather: Success! The new status is: DISABLED. /help
By default A Bot will receive only messages addressed to it by any user directly via posting by /command#YourBot any message you send.
After that it vill be available via getUpdates API call.
In browser it will be:
https://api.telegram.org/botToken/getupdates
Find the related message in output JSON and grab chatId. It will allow you to answer back with:
https://api.telegram.org/botToken/sendmessage?chat_id=123456788&text=My Answer
Make your bot by admin in group.
You can access all avaliable settings from all of your bots by sending /mybots to Botfather. Choose the bot, then Bot Settings and Group Privacy. If its disable (default), you can tap on Turn off.
Now its possible to receive the chat history using GetUpdates. This can be done via HTTP API or the frameworks. For example, in C# (.NET Core) like this:
var bot = new TelegramBotClient(ApiToken);
var updates = bot.GetUpdatesAsync().Result;
foreach(var update in updates) {
Console.WriteLine($"{update.ChannelPost.Date} {update.ChannelPost.Text}");
}
But keep in mind that this feature has some kind of perfect forward secrecy implemented. So you only get messages that were send after group privacy is disabled. As a result, the GetUpdates result is empty until some post was made.
if you added your bot before disable the privacy mode, you should remove the bot from the group and add it again

Personal notifications in slack messages sent by Python bot are not highlighted

I created a small test bot connecting a local python script with Slack. The main task of the bot is to create a custom string, and send it to a channel in Slack. This is done with
from slack_sdk import WebClient
client = WebClient(token = slack_token)
slack_message = "Hello #demo_user" # #demo_user is an existing user, which can be mentioned in chat
client.chat_postMessage(channel = channel_ID, text = slack_message)
In this message I would like to explicitly mention certain groups or persons by using #<username>. This works fine if I type out the message directly in Slack, but if the bot itself sends the message, the groups are not messaged/pinged, even though the message contains the corresponding handle. What is happening here, and how can I ping those people/groups instead via Python?
I'm using the Slack API with NodeJS, but it might come from the link_names argument. Did you try to set it to true?
https://api.slack.com/methods/chat.postMessage#arg_link_names

Updating Telegram Bot Commands In Realtime

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.

How to send query to telegrambot by clicking a link, not a button?

I built a telegrambot in Python. I managed to send a keyboard with a message (allowing me to send callback queries to the bot) using this example.
Now, if I only have one option, these buttons are a bit large in my opinion which is why I'd like to find another solution to send a callback query to the bot. Is there a way to send such a query by clicking a link inside a message? From the docs, I don't see an obvious way to do that.
To provide more context: I built a command /reminder to schedule and send reminders (as Telegram messages, see attached screenshot).
When a reminder is scheduled, the bot will send a message saying that the reminder was scheduled. In this message, I'd like to append a link "Delete" in the text (not as InlineKeyboardButton, because such a button would make this "FYI, I scheduled the reminder, just as you requested" message too large in my opinion) and clicking this link shall send a query to the bot which I then can use to remove the correct reminder from the jobqueue and database.
The last message in the screenshot (when the reminder is actually sent and can be rescheduled via buttons) is only to show that the buttons indeed significantly increase the vertical space of such a message.
Add MessageHandler to list of handlers.
updater.dispatcher.add_handler(MessageHandler(Filters.text, some_handler_method)])
So update from buttons will go to CallbackQueryHandler and typed text to MessageHandler.

Can a Slack bot interact with another bot and trigger some functions of it in a channel?

I built a Slack bot and tried to make my bot interact with another bot in the a channel, but it seems not working.
For example, I want to use the voting function of Polly (a Slack bot).
Regular users like me send /polly "Which is better?" "Tacos" "Pizza" message and Polly will create a Slack poll in a channel. But when I made my bot send the same message in the same channel (I use python-slackclient and chat.postMessage method), the message just like a simple text, in other words, it didn't trigger Polly.
So, in a channel, how can a Slack bot interact with another bot and trigger some functions of it?
Did anybody ever do something like this?
update
https://github.com/ErikKalkoken/slackApiDoc/blob/master/chat.command.md
I tried this method but got another problem...
The error message is
{'error': 'missing_scope',
'needed': 'post',
'ok': False,
'provided': 'identify,bot:basic'}
The Oauth token requires "post" scope, but official documents show that "post" scope is deprecated. How do I make my token have "post" scope?
I have tried to make two bots interact and didn't find it to work. Slack somehow recognizes the source of the message and if the message is sent by a bot or an app, it fails to respond to it. I have even tried to post the message as a user through the slack API but did not get it to work.
However, Bots can use the chat.command method to invoke a slash command.
Unofficial documentation can be found here:
https://github.com/ErikKalkoken/slackApiDoc/blob/master/chat.command.md
You are correct that the undocumented chat.command requires the post scope to work, which is not available in the standard OAuth process (e.g. you can not choose it as scope on the Slack app config site.)
The only currently working solution that I know of it to use a legacy token.
See also this answer.

Categories