设置Activity.Typing动画的时间 [英] Set time of Activity.Typing animation
问题描述
我正在尝试从服务器中获取数据时创建一些动画. 打字"活动似乎是合理的,但只能持续约4秒钟:
I'm trying to create some animation during the time when I fetch the data from a server. "Typing" activity seems to be reasonable but it works only for ~4 seconds :
Activity reply = activity.CreateReply();
reply.Type = ActivityTypes.Typing;
reply.Text = null;
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
await connector.Conversations.ReplyToActivityAsync(reply);
我正在尝试进行异步监听:
I was trying to do async listening:
while (!_fetchEnded)
{
await connector.Conversations.ReplyToActivityAsync(reply);
Thread.Sleep(3000);
}
但是,它会产生缓慢的行为.是否可以设置键入"活动的持续时间或其他方法来防止打开和关闭键入?
But bot it creates laggy behaviour. Is there a possibility to set the duration of "typing" activity or another way around to prevent turning the typing on and off?
推荐答案
默认情况下,键入仅显示几秒钟.您可以通过以较低的频率再次发送打字事件来迫使显示打字指示器更长.
Typing is displayed only a few seconds by default. You can force the display typing indicator longer by sending again typing events at a lower frequency.
实施示例,它将每2秒发送一次事件,最长30秒:
Implementation example, where it will send events every 2 seconds, for 30 seconds max:
public async Task<HttpResponseMessage> Post([FromBody]Microsoft.Bot.Connector.Activity activity, CancellationToken token)
{
// Send Typing messages
var typingCancellation = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var typingTask = SendTypingActivityUntilCancellation(activity, TimeSpan.FromSeconds(2), typingCancellation.Token);
try
{
// Activity treatment
if (activity.Type == ActivityTypes.Message)
{
// ...
}
else if (activity.Type == ActivityTypes.Event && activity.ChannelId == ChannelEnum.directline.ToString())
{
// ...
}
typingCancellation.Cancel();
await typingTask;
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception e)
{
typingCancellation.Cancel();
await typingTask;
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
public async Task SendTypingActivityUntilCancellation(Activity activity, TimeSpan period, CancellationToken cancellationtoken)
{
try
{
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity isTypingReply = activity.CreateReply();
isTypingReply.Type = ActivityTypes.Typing;
do
{
if (cancellationtoken.IsCancellationRequested == false)
{
await connector.Conversations.ReplyToActivityAsync(isTypingReply);
}
// Check again if token has not been canceled during the reply delay
if (cancellationtoken.IsCancellationRequested == false)
{
await Task.Delay(period);
}
}
while (cancellationtoken.IsCancellationRequested == false);
}
catch (OperationCanceledException)
{
//nothing to do.
}
}
这篇关于设置Activity.Typing动画的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!