简体   繁体   中英

Is there a way to “flush” the messages into Facebook Messenger?

TL;DR : the bot takes 2-9 seconds to send any message to the user, even if I send the message from the very first line in MessagesController.cs .


I have used the Bot Framework to create a couple of bots for Facebook Messenger and I noticed that one of them is significantly slower than the other.

Both bots have the code below as the very first lines in the MessagesController.cs . This is supposed to send the Typing indicator to the user.

One bot consistently takes 2 seconds to show this typing indicator to the user on Facebook, while the other takes 9 seconds.

            Activity typing = activity.CreateReply(null);
            typing.ServiceUrl = activity.ServiceUrl; 
            typing.Type = ActivityTypes.Typing;
            ConnectorClient connector = new ConnectorClient(new Uri(typing.ServiceUrl));
            await connector.Conversations.SendToConversationAsync(typing);

The second bot indeed does much more work (calling various web APIs), but since these lines are the very first in the controller, I expect the typing indicator to be sent to the user immediately, after which the bot can continue doing its work.

However, it seems that the messages (including the typing indicator) are not sent until the bot completes its work.

Is there a way to "flush" the messages to the user to have a typing indicator sent immediately, or am I encountering some other issue?


Update: I've tried the ConnectorClient.Dispose() method but it doesn't seem to help make it any faster to send messages.

I am not sure what you mean by "flushing" the messages to the user but your bot should show the typing indicator immediately until you type a message and there is some process in the background. To avoid processing information until the user types a message you can use the ActivityTypes like this:

        if (activity.Type == ActivityTypes.Message)
        {
            Activity typing = activity.CreateReply(null);
            typing.ServiceUrl = activity.ServiceUrl; 
            typing.Type = ActivityTypes.Typing;
            ConnectorClient connector = new ConnectorClient(new Uri(typing.ServiceUrl));
            await connector.Conversations.SendToConversationAsync(typing);
        }
        else
        {
            HandleSystemMessage(activity);
        }
        ...

However, it makes sense that the user cannot type anything until the application has already given a answer back to the previous question.

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