1
votes

I'm using the Microsft BotFramework.

Process: the clients ask my bot to generate a specific code 1. The bot answer to the client that he is generating the code. 2. After about 10 seconds, the bot send the code to the client, without any other request.

Problem: I'm using the

ReplyToActivityAsync(...)

method to send both answers, before the return statement. In that case there is a post timeout error between the 2 answers.

That's my code:

        if (activity.Type == ActivityTypes.Message)
        {
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

            // return our reply to the user
            string welcomeMessage = "[...] Reply 1 [...]"
            await connector.Conversations.ReplyToActivityAsync(activity.CreateReply(welcomeMessage));

            // MyApi.GetCode() takes about 10 secs
            await connector.Conversations.ReplyToActivityAsync(activity.CreateReply(MyAPI.GetCode()));
        }

How to start a reply without waiting for a user request ? Thanks !

1
What happens if in the second call instead of using ReplyToActivityAsync you usea SendToConversationAsync?Ezequiel Jadib
Exactly the same.Jonathan
Yes..I imagine that. And if instead of sending MyAPI.GetCode(), you send something else like "hello", it just works right? I suspect the problem is in your GetCode method... is there any change to make that method async?Ezequiel Jadib
The problem is that i'm in a "Post" Context (Post action of the web api controller) and now i'm waiting the 2nd message to close the Post context, that's why i've got a Post timeout error. I think the solution can be starting in background the MyAPI.GetCode() task and sending it to user... Do you have an idea in that purpose ?Jonathan
I'm I replicated your scenario (creating a method that takes time to complete) and it's working as expected. I even put a delay of 50 seconds and it's still working; so it should be something with MyAPI.GetCode(). That cals probably need to be awaited too.Ezequiel Jadib

1 Answers

0
votes

You can send a message using recipient's id without user's request like the following. Therefore, you can send a message to user after getting successful response from your api calling and with delay, also in another function.

string userId ="123456789"; // For Example
string serviceUrl = "https://telegram.botframework.com"; // For Example

var connector = new ConnectorClient(new Uri(serviceUrl));
IMessageActivity newMessage = Activity.CreateMessageActivity();
newMessage.Type = ActivityTypes.Message;
newMessage.From = new ChannelAccount("<BotId>", "<BotName>");
newMessage.Conversation = new ConversationAccount(false, userId);
newMessage.Recipient = new ChannelAccount(userId);
newMessage.Text = "<MessageText>";
await connector.Conversations.SendToConversationAsync((Activity)newMessage);

In the above, you can send a message to a user using his Id in the related channel. Also, serviceUrl is defined based on the related channel (here, Telegram is as an example).