简体   繁体   中英

ConversationUpdate doesn't trigger after publish

Hi I created my first test bot using Microsoft BotFramework in C#. in private async Task< Activity > HandleSystemMessage(Activity message) in if (message.Type == ActivityTypes.ConversationUpdate) normally it should notify a new member added to group or someone hit the start button of bot in Telegram Messenger. When I test it in debug mode using BotFramework emulator everything works perfectly but after I publish it I see that after hitting start button in Telegram messenger my code didn't run. My code in ActivationType.ConversationUpdate

foreach (var item in message.MembersAdded)
                {
    try
    {
        using (var dbcontext = new WatermarkBotDBEntities())
        {
            dbcontext.BotUsers.Add(new BotUser()
            {
               AddedFriends = 0,
               ConversationID = message.Conversation.Id,
               ServiceUrl = message.ServiceUrl,
               UserID = message.From.Id
            });
         dbcontext.SaveChanges();
         if (Request.RequestUri.Query != "")
         {
             var u = dbcontext.BotUsers.Where(x => x.BotSalCode == Request.RequestUri.Query.Replace("?start=", string.Empty)).FirstOrDefault();
             u.AddedFriends++;
             dbcontext.Entry(u).State = System.Data.Entity.EntityState.Modified;
             if (u != null)
             {
                 var connector = new ConnectorClient(new Uri(u.ServiceUrl));
                 IMessageActivity newMessage = Activity.CreateMessageActivity();
                 newMessage.Type = ActivityTypes.Message;
                //newMessage.From = new ChannelAccount("<BotId>", "<BotName>");
                newMessage.From = new ChannelAccount("c3e7mhdafcecn7ng3", "Bot");
                newMessage.Conversation = new ConversationAccount(false, u.ConversationID);
                newMessage.Recipient = new ChannelAccount(u.UserID);
                if (u.AddedFriends <= 2)
                    newMessage.Text = $"SomeText.";
                else newMessage.Text = "SomeTex";
                await connector.Conversations.SendToConversationAsync((Activity)newMessage);
                 dbcontext.SaveChanges();
           }
        }
    }
}
catch (Exception ex)
{
}

So how is it possible to detect hitting start in telegram ? Regards

I realize this is not a complete answer, but I wanted to share this code with you in case it may help. Below is the recommended way to send a welcome message, you may be able to repurpose this code for your use.

else if (message.Type == ActivityTypes.ConversationUpdate || message.Type == ActivityTypes.Message)
            {
                IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
                if (iConversationUpdated != null)
                {
                    ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));

                    foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
                    {
                        // if the bot is added, then
                        if (member.Id == iConversationUpdated.Recipient.Id)
                        {
                            var reply = ((Activity)iConversationUpdated).CreateReply(
                            $"Hi! I'm Botty McBot.");
                            await connector.Conversations.ReplyToActivityAsync(reply);
                        }
                    }
                }
            }

This is the answer I found for my question after lots of testing : In MessagesController class in public async Task<HttpResponseMessage> Post([FromBody]Activity activity) function that defined by default in a BotFramework Application you have to do something like this :

if (activity.Type == ActivityTypes.Message)
{
    if (activity.Text.StartsWith("/start"))
    {
        //This will return you the start parameter of a link like : http://telegram.me/botname?start=Parameter
        var Parameter = activity.Text.Replace("/start ", "");
    }
}

and if you want to send a welcome message so you can surely use the way that @JasonSowers told and use his code to send your message . Best Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM