[Telegram] Completely failing to process ConversationUpdate for Telegram bot
So I am writing this very simple test bot that will say "hello" and "bye" when people join/leave group. I've just created a new project from bot template and substituted handling of Message activity with ConversationUpdate activity:
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.ConversationUpdate)
{
ConnectorClient connector = new ConnectorClient(new Uri("https://telegram.botframework.com"));
var replyMessage = String.Empty;
if (activity.MembersAdded != null && activity.MembersAdded.Any())
replyMessage = "Hello!";
else if (activity.MembersRemoved != null && activity.MembersRemoved.Any())
replyMessage = "Bye!";
// return our reply to the user
Activity reply = activity.CreateReply(replyMessage);
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
My first problem was that ConversationUpdate doesn't have ServiceUrl set, so I had to copy it from Message activity and hardcode. But even after that it wouldn't work. It gets the ConversationUpdate activity update, it finds the joined/left members, create reply and sends it without any exception. But there is no activity from the bot in the channel.
This works fine if I publish my bot as it is with the code from template. It handles messages and replies how many characters the user has typed. But there are no replies when people join/leave.
@eanders-MS it seems that telegram channel doesn't send ConversationUpdate when user added/removed from the group.
@msft-shahins I do get the ConversationUpdate, but when I try to create reply out of that activity and send it back, it never reaches group. I have found a workaround though. Instead of creating a reply activity from the incoming activity, I create it manually. I put my bot into From field and I also copy Conversation from incoming ConversationUpdate activity. When I send this, it reaches the group successfully:
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
var connector = new ConnectorClient(new Uri("https://telegram.botframework.com"));
if (activity.Type == ActivityTypes.ConversationUpdate && activity.MembersAdded != null && activity.MembersAdded.Any())
{
var tBot = new ChannelAccount
{
Id = "mybot",
Name = "mybot",
};
var name = string.Empty;
if (activity.MembersAdded != null && activity.MembersAdded.Any())
name = activity.MembersAdded.First().Name;
var reply = new Activity
{
From = tBot,
Type = ActivityTypes.Message,
Conversation = activity.Conversation,
Timestamp = DateTime.Now,
Text = $"@{name} greetings!"
};
await connector.Conversations.SendToConversationAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
I am not sure if this is a valid way to do it though. I've also noticed that I get lots of warnings on my web app with the following message: "2016-12-09T19:58:46 PID[22380] Warning Service url is not a well formed Uri!"
Also this is all happening on v3.3.0. I've tried to update to v3.3.3 but in that version if I try to hardcode telegram connector URI, I get an error that that URL is not secure and it stops working altogether.
@ncksol I looked at the incoming ConversationUpdate activity when members added/removed from telegram groups and the main bug here is that ServiceUrl coming from connector service is empty when ConversationUpdate is sent to bot in telegram channel. @eanders-MS can you take a look?
Since BotAuthentication attribute automatically adds the ServiceUrl in the attribute to the set of trusted service urls after successful authentication, as a workaround for now, you can add the "https://telegram.botframework.com" to set of trusted service urls by calling MicrosoftAppCredentials.TrustServiceUrl and then follow the logic that you have implemented to send the welcome message.
@ncksol This was indeed a bug in our Telegram adapter. Thank you for reporting it. A fix is in the works.
@ncksol This should be fixed now. Let me know if you're still having trouble.
@eanders-MS cheers! I am currently trying to write a fancy bot for telegram, so there might be more bugs incoming =D I'll try to contribute as much as I can, if I can fix them myself too.
@ncksol It looks like serviceUrl is again not getting populated. Looking into it.
@ncksol The Telegram channel was setting an incorrect value for message.address.bot.id on conversationUpdate messages. Additionally, it was not setting message.address.bot.name. Until the fix for this issue is live, you can work around it by manually setting the values:
message.address.bot.id: The username of your bot on Telegrammessage.address.bot.name: The botId of your bot on the Bot Framework.
Sample workaround:
bot.on('conversationUpdate', (message) => {
const address = message.address;
address.bot.id = "<TELEGRAM BOT USERNAME>";
address.bot.name = "<BOT FRAMEWORK BOTID>";
if (message.membersAdded) {
message.membersAdded.forEach((member) => {
const reply = new builder.Message()
.address(address)
.text('hello ' + member.name);
bot.send(reply);
});
}
if (message.membersRemoved) {
message.membersRemoved.forEach((member) => {
const reply = new builder.Message()
.address(address)
.text('goodbye ' + member.name);
bot.send(reply);
});
}
});
@eanders-MS I've just updated to v3.4.0 and noticed that when I create reply from conversationUpdate activity, the From is populated with my bot's id in ID field, but that ID is not the telegram ID, but the ID of my bot in Bot Framework. Also name is not populated at all. And the reply doesn't get posted into the channel unless those two properties are correctly populated. I can hardcode those for now I guess.
Yes, I think we are saying the same thing. Hard-coding the bot name and id fields is the workaround until a fix for this issue makes it to production. Apologies for the inconvenience, and thank you for taking the time to report this.
@eanders-MS was this fix shipped to production?
@carlosscastro No, not shipped yet.
@ncksol are you still experiencing the issue after upgrading to BotBuilder v3.8.1?
@eanders-MS Has this been released yet?