How do I return a Typing activity from my v4 Bot

我只是一个虾纸丫 提交于 2020-03-23 11:34:31

问题


I'm trying to add a Typing activity to a long-running action in my bot, but I keep getting a "BadGateway" error. Most of the samples I've found seem to be for bot framework v3, so the types or methods don't appear any more, and I've tried a few options for v4 (using C#), like the following:

await turnContext.SendActivityAsync(new Activity() { Type = ActivityTypes.Typing });

or

var typingActivity = new Activity()
{
    Type = ActivityTypes.Typing
    //RelatesTo = turnContext.Activity
};

typingActivity.ApplyConversationReference(typingActivity.GetConversationReference());

or

var act2 = MessageFactory.Text(null);
act2.Type = ActivityTypes.Typing;
await turnContext.SendActivityAsync(act2);

all of these result in a BadGateway error.

Can someone guide me on where I'm going wrong?


回答1:


Your implementation is close, but needs a couple minor adjustments. Also, the text property is optional. If it's not needed, then you can simply remove it (same for the delay). This is what I use which adheres to the documentation (variable is used to match your code). You can reference the docs here.

var typingActivity = new Activity[] {
    new Activity { Type = ActivityTypes.Typing },
    new Activity { Type = "delay", Value= 3000 },
    //MessageFactory.Text("Some message", "Some message"),
};

await turnContext.SendActivitiesAsync(typingActivity, cancellationToken);

Hope of help!




回答2:


The answer of Steven Kanberg has the right code, but unfortunately this is a service issue at the moment, as confirmed in this issue on Github.

When the issue is resolved, it should be posted in the Github issue above.




回答3:


Please try out this code to send a typing activity from your bot:

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
   var typingActivity = MessageFactory.Text(string.Empty);
   typingActivity.Type = ActivityTypes.Typing;
   await turnContext.SendActivityAsync(typingActivity);
}


来源:https://stackoverflow.com/questions/60710065/how-do-i-return-a-typing-activity-from-my-v4-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!