Reply to a message with a webhook - discord.py

I have this code to send a message by a webhook. I want to reply to the same message, but it's not working as expected. Is there any way to add a reply message through webhook to the same message sent before by webhook?
webhook = await channel.create_webhook(name=message.author.name)
msg = await webhook.send(content = str(result.text))
await msg.reply('TEST')

As of today, replying to messages using webhooks is not possible.
https://github.com/discord/discord-api-docs/issues/2251
I doubt it'll never be implemented, see my comment
https://github.com/discord/discord-api-docs/discussions/3282#discussioncomment-1691383

To get the actual message when sending it with a webhook you need to add the wait=True keyword argument.
webhook = await channel.create_webhook(name=message.author.name)
msg = await webhook.send(content=str(result.text), wait=True)
await msg.reply('TEST')

Related

What's the right way to handle message reactions in a notification only bot?

I have created a one way notification only bot in Teams (only personal scope), I am able to send proactive messages but however, when someone reacts to a message, Teams is showing a notification for the message which was reacted to. How do I prevent this behavior and just silently ignore the message reaction. I was hoping since it's a one way notification bot, there would be an option to disable it, but apparently there isn't.
I have a PHP REST API endpoint which is configured to be the bot endpoint address. This API is pretty basic and handles only certain types of requests like installationUpdate. For all other types, it just sends a HTTP 200 response with an empty body.
When the user first installs the App in teams, I am storing the conversationId, tenantId and the serviceUrl and later use these values to send notifications (proactive messages) when certain events happen in a web application. These are sent via a C# Console Application.
When a user reacts to a message, I get a request with the type messageReaction, this is where I am unable to figure out how to handle this so that the message reaction is ignored and does not cause a notification in Teams.
This is what my PHP REST API (bot endpoint) looks like
function onBotRequest() {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
http_response_code(404);
return;
}
$requestJson = json_decode(file_get_contents('php://input'), true);
if ($requestJson['channelId'] != 'msteams') {
http_response_code(404);
return;
} elseif ($requestJson['type'] == 'installationUpdate') {
$serviceUrl = $requestJson["serviceUrl"];
$conversationId = $requestJson["conversation"]["id"];
$tenantId = $requestJson["conversation"]["tenantId"];
if ($requestJson['action'] == 'add') {
// App installed
// Store conversationId, tenantId, serviceUrl in db
} elseif($requestJson['action'] == 'remove') {
// App uninstalled
// Remove conversationId, tenantId, serviceUrl from db
}
} elseif ($requestJson['type'] == 'messageReaction') {
// What should be sent as the response here to ignore the message reaction?
}
header('Content-Type: application/json');
http_response_code(200);
}
The code used for sending proactive messages
var credentials = new MicrosoftAppCredentials(appId, appPassword);
var connectorClient = new ConnectorClient(new Uri(serviceUrl), credentials);
var response = await connectorClient.Conversations.SendToConversationAsync(conversationId, activity);
I tried sending different HTTP status codes like 400 but irrespective of the response status code, the notification still occurs. I guess I am missing some required params in the response body, but I couldn't find any documentation.
Removing the call to TeamsNotifyUser will prevent Teams sending notifications when message reactions are added/removed.

Discord.py: How to pin a post made by a webhook?

Message= await webhook.send("Test message to pin")
await Message.pin()
But I get a str in return from webhook.send. How can I get a message type so I'm able to pin it?
Thank you in advance.
If you read the documentation. You will see that you need to add the following parameter: wait=True (when sending through a webhook). This will return the message. If you dont do this it will return None as explained in the doc.
Thus what you need to do:
Message = await webhook.send("Test message to pin", wait=True)
await Message.pin()

ServiceUrl "https://smba.trafficmanager.net/apac/" is not working

I have a MS Teams bot. I have installed the bot for couple of users in tenant. Now when I'm starting conversation, for few users it is responding and few it is not.
I investigated further and found out that for users who are getting reply from bot, the serviceurl is "https://smba.trafficmanager.net/in/".
For users who are not getting reply from bot, the serviceurl is "https://smba.trafficmanager.net/apac/".
Exception message: Operation returned an invalid status code 'NotFound'
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
var reply = activity.CreateReply();
reply.Text = "Hi there";
await context.PostAsync(reply);
}
This sounds like it's possibly a TrustServiceUrl Issue (despite the 500 vs 401 error message).
You can fix it by adding all of your ServiceUrls to the list of trusted URLs:
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
var serviceUrl = activity.ServiceUrl;
MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
var reply = activity.CreateReply();
reply.Text = "Hi there";
await context.PostAsync(reply);
}
This should ensure that your bot "trusts" the ServiceUrl of any message that it receives.
Let me know how that goes. I'm 90% sure this is the issue, but it might not be.
Here's a link to the library, if that helps. Otherwise, browsing these issues should help.
Note to others:
This "Trust Service URL Issue" doesn't apply to just Teams. This happens for lots of other URLs when trying to use Proactive messaging. Just replace serviceUrl with whatever is appropriate for your use case. And yes, if you're using multiple channels, you can add multiple URLs when using MicrosoftAppCredentials.TrustServiceUrl() by calling it multiple times.
Here's the method definition. Note: you can add expiration for this, as well.
I've submitted a PR for this, which so far has resulted in some updated docs

MS Teams - Don't show notification of specific message in the activity feed

Question
I have a simple Bot for MS Teams developed in C# with the Bot Builder SDK 3.15.0.0 targeting .NET framework 4.7.1.
When mentioned, it retrieves the Jira ticket Ids in the message and returns a single reply with a list of Cards, each one displaying a summary of a Jira Issue.
I'd like to know if it's possible to not populate the activity feed when sending the reply with the card attachments as it's not needed for my use case.
Example
This is how I usually build the reply to a user message
var reply = activity.CreateReply();
reply.AttachmentLayout = AttachmentLayoutTypes.List;
reply.Attachments = thumbnailCards;
await context.PostAsync(reply);
And this is what I tried after reading the docs at https://learn.microsoft.com/en-us/microsoftteams/platform/concepts/activity-feed#rest-api-sample
var reply = activity.CreateReply();
reply.AttachmentLayout = AttachmentLayoutTypes.List;
reply.Attachments = thumbnailCards;
reply.ChannelData = JsonConvert.SerializeObject(new
{
notification = new
{
alert = false
}
});
await context.PostAsync(reply);
I was hoping that setting the ChannelData with notification.alert = false would just disable the notifications, but it actually doesn't display any message.
Have you tried using the Teams nuget package: https://www.nuget.org/packages/Microsoft.Bot.Connector.Teams
var reply = activity.CreateReply();
reply.ChannelData = JObject.FromObject(new TeamsChannelData()
{
Notification = new NotificationInfo(false)
});
Source for this package can be found here: https://github.com/OfficeDev/BotBuilder-MicrosoftTeams/
The alert you are getting in the activity feed is simply the "someone replied to your message" alert and is nothing special coming from the bot. This notification in the activity feed cannot be disabled as of now. Other team members won't receive this alert in activity feed unless they are following the same channel.
Sending notification using Rest API is designed to work for 1:1 chat.

Botframework hide messages in group chat

How in group chat
hide message to bot -- #mybot xxx?
send message from bot only to one user of group?
None of the channels that support bots being members of a group allow users to explicitly block a bot from getting messages, though some require that the bot is #mentioned in order to get any message sent in a group.
For those that support Direct Messages, the Bot can send a message to a single user as follows:
var response = await activityContext.ConnectorAPI.Conversations.CreateDirectConversationAsync(activity.Recipient, activity.From);
var reply = activity.CreateReply($"This is a direct message to {activity.From.Name ?? activity.From.Id} : {activity.Text}");
reply.Conversation = new ConversationAccount(id: response.Id);
reply.ReplyToId = null;
await activityContext.ConnectorAPI.Conversations.SendToConversationAsync(reply);

Resources