Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

.net - Bot Framework - Multiple delayed answers

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...