Message a single user by user id - python

I am looking at this sample: https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/python/57.teams-conversation-bot/bots/teams_conversation_bot.py
_message_all_members iterates thorough a list of all members and sends each one a message.
I need a way to send one message to one user. I know their user id. Can you help me write a function that takes a text message and a user id and sends it. Whoever wrote that sample is a pervert, and I can't unwrap all the layers. tc1, tc2. wtf?

I think what you're looking for is "proactive notification", a method to send an activity to a user without the bot first receiving a message from them?
The thing that makes this awkward is that to use the SDK, you need to have a TurnContext that represents the conversation between the bot and the user... this is typically done using the adapter.continue_conversation method which requires you to pass the conversation reference. The conversation reference itself can either be cached from a previous bot message from that user or obtained by creating a new conversation with that user (this would be using the adapter.create_conversation method).
The tc1, tc2 stuff are essentially just references to turn contexts in nested callback methods as this is how create_conversation and continue_conversation work.
There is a specific proactive notification sample that might work better for you here: https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/python/16.proactive-messages

Related

Is it possible to use aiogram to send messages without handlers?

Recently I've been writing bot which parses site and sends message to users, whenever new information appears. Is it possible to make it on aiogram? Thank you for your answer in advance
Yes. it is.
But you have to store user_id of your users, then when "information appears" you take from your storage user_ids and send message to each user. Also it is unreal to send message for all users in one time due to telegram limits (see here).

Python Bot API: Set messages to read after some time

I have a bot which polls data regularly from some input and if a specific logic applies, sends a message to a telegram channel.
The messages are only relevant for readers if the reader in the channel more or less directly read the message, i.e. after let's say 30 min, the message is irrelevant. I do not want to delete the message for historic purposes, BUT I'd like to set it to read for everyone.
Is there a way to set the "read"-status for the receivers? Is it possible to do so, i.e. change, for already sent, i.e. previous messages?
To avoid having to keep track of sent messages with their timings, is it even possible to specify that directly when sending the message?
No, this is not possible. The read status can only be changed by the user itself. Anything not mentioned in the official API docs at https://core.telegram.org/bots/api can not be done with Telegram Bots.
As a side note: AFAIK the read status is not binary for each user. This can be observed by noting that viewing a channel message from different devices/clients with the same account will up the view count by more than 1.

What is an envelope pattern in Pubsub messaging?

I am looking for some resources in understanding the envelope pattern and google isn't being too helpful. Can someone illustrate this with an example? For more info, I am working with Pubsub in Python and I'm looking to tag messages with some additional info (apart from plainly adding that info in the message body) for complex applications.
The envelope pattern is a way to wrap a message with metadata to help determine what to do with that message without having to understand the contents of the message itself. The metadata can be used to determine how to route a message to the appropriate receiver to deal with the message contents or it could indicate what to do with the message itself.
Typically, the way this pattern works is that different layers add a wrapper on top of a message that is passed to it. There can be multiple, nested envelopes. When a message is received, there is a corresponding layer that removes one of the envelopes for every layer that added one.
The side that creates messages would look like this:
The side that consume messages would look like this:
A Google Cloud Pub/Sub message has an attributes map that can be used for precisely this purpose. You can put data in the attributes that can indicate what to do with the message without having to decode the bytes stored in the data portion of the message.
For example, imagine you have a topic that contains messages that correspond to different user actions on an ecommerce website. Some of the messages are for profile updates while others are transactions. You want to perform different actions on these types of messages. Instead of looking into the data field in the message to determine which type it is and what to do with it, you could add key/value pairs in the attributes, e.g. action: UPDATE and action: TRANSACTION. Then, your subscriber would look at the value for the action attribute and determine what to do with the message. It could execute a different piece of code or maybe even send it to a different service, acting as a router for messages. The key point is that the determination of what to do could be performed without having to decode the message data itself.
Messages sent to push subscription endpoints have an additional envelope added that indicates the subscription the message came from. This is necessary because it is possible for multiple push subscriptions to point to he same endpoints.
The PubSub envelope pattern is simple
The message content (absolutely what you want, base64 encoded)
Attributes on the message (that you can add to add meta data on the content). You can use attribute for taking decision on the message without processing it. For exemple, add a message version. If your code can only process v1 and you have a v2 message, skip it instead of having unmarshalling error.
Id of publication
Timestamp
When you consume the envelop, it is presented in 2 differents manner:
Cloud Function PubSub trigger event
Push Subscription
You will find the same information but not structured with the exact same JSON schema.

Forward a message with multiple media files using bot Telegram API

I'm having a problem forwarding messages with multiple media files (in my particular case, photos).
I'm using forwardMessage method to forward the message that user sends to bot (which, in some cases, might contain more than one photo):
bot.forwardMessage(admin_id, from_chat_id, message_id)
But, the thing is, according to Telegram API v3.5, the message which contain media group breaks up into array of messages, and hence the bot breaks up the message from user into multiple messages. E.g. if user have sent a message with 3 photos, then the bot forwards 3 messages, each of which contains a photo.
What I want is to forward a message with multiple media files as it is, as one message, not as multiple messages.
I know that probably I can take each photo from each message that the bot receives, put them one-by-one into array, and then send it from the bot using sendMediaGroup method, but I'd like to know if I can get it working with forwardMessage method.
Thanks in advance.
You can't forward original content via forwardMessage, for instance, you can't forward with an inline keyboard.
I think this may be a mistake, you can suggest that to #BotSupport.
Telegram API had updated a method call sendMediaGroup for sending images as album.
https://core.telegram.org/bots/api#sendmediagroup
Have a look :)

Facebook messenger chatbot gives duplicate replies

I'm building a chatbot with wit.ai and python and integrating it with Facebook Messenger. I have set up webhooks as well as send function and fb_message function exactly like in this tutorial: https://github.com/wit-ai/pywit/blob/master/examples/messenger.py
My bot works fine when I test it in wit.ai or in console, but when I test it in actual Facebook Messenger, it gives each reply twice. It doesn't matter which function inside the bot gets called, I always get two replies.
My most simple action is this:
def defaultAnswer(request):
context = request['context']
return context
(This returns always the same string, which is defined in wit.ai engine.)
If your bot is subscribed to events other than pages_messaging, like messaging_deliveries, make sure you're handling this event as well.
What's happening is your that bot is receiving the message request, then receiving another one by another event, and your code is handling them both as messages.
I think this is the problem, and if it's not, give us more details about it.
Someone might have a case like mine.
My case:
I mistakenly subscribe my two different bots to one facebook page on the developer.facebook.com.
Due to the this mistake, when i write a specific command(e.g. restart) to my facebook page, bot replies exactly same two messages(e.g. "Bot Restarted", "Bot Restarted") to me.
It made me a crazy to find out the problem.
You should subscribe one bot to your facebook page at most.
I had this issue before and it turns out, the message_echoes was enabled. What happens essentially if this is enabled is that the Bot will try to echo whatever you text you entered.
My logic ended up catching the echo and then responded with the same response. I do have a handler to catch similar responses so I was able to avoid the infinite loop.

Categories