我有基于C#SDK的Azure Bot开发并在Azure上部署,该机器人正在使用语言(自定义询问)服务来回答用户问题。
前端部分是SPFX React Webchat,它假定是另一个SharePoint网站的网页。
该机器人在模拟器或测试网络聊天(在门户网站)中工作正常,但是我在从SPFX React Webchat运行它时面临重复的欢迎消息问题。
React Webchat的过程如下:
-
componentDidmount()挂钩i请求令牌直接令牌:
public async componentdidmount(){
this.fetchtoken();
}
私有async fetchToken(){
var mytoken ='<< - 秘密键 - >>';
const res =等待fetch('https://xxxx.azurewebsites.net/api/getltoken',
{方法:'get',
标题:{'Accept':'application/json'}
});
var {token} =等待res.json();
console.log(token);
this.setstate({
DirectLine:等待CreateCtirectlineAppServServiceExtension({{
域:'https://xxxx.azurewebsites.net/.bot/v3/directline',
令牌,
}),
});
this.state.directline.postactivity({
来自:{id:“ serid”,name:“ user_name”},
名称:“ requestwelcomedialog”,
类型:“事件”,
价值:“令牌”
})。订阅(
id => console.log(`已发布活动,分配的ID $ {id}`),
错误=> console.log(`错误发布活动$ {error}`)
);
}
成功获取令牌。
-
on
on public render():react.reaeactelement
方法i load < reactwebchat directline = {this.state.directline
},它包裹在面板下(Office UI UI Fabric面板)
public render():react.reeactelement
方法i加载< reactwebchat directline = {this.state.directline
},该},该 p>在onembersaddedAsync方法上的bot源代码中,我将添加的成员ID与TurnContext收件人ID进行了比较,如下所示,如果不是一样,我显示欢迎卡(自适应卡):
保护覆盖异步任务onmembersaddedAsync(iList成员,iturnContext Turncontext,concellationToken concellationToken)
{
foreach(成员添加的var成员)
{
if(Member.ID!= TurnContext.Activity.Recipient.ID)
{
等待sendwelcomemessageasync(TurnContext,cancellationToken);
}
}
}
整个过程和与机器人的通信绝对效果很好,但是我无法追踪问题,为什么欢迎屏幕会反复加载我们的定期时间。
我发现的调试期间的一个观察结果是成员。ID是我在令牌生成过程中生成的GUID,TurnContext Receipent ID与 xxx-bot-t-xxs@ktxxxb7dbjms 一样有所不同。 Strong> App Service@一些随机ID ,我真的不知道是如何生成的。
因此,可能是原因比较案例总是失败并显示欢迎卡。
任何有助于简化该过程的帮助,因为我是SPFX的新手,并且特别反应,我不知道如何控制客户端代码。
作为参考,我在此处附加了屏幕截图。预先感谢您。
I have C# SDK based azure bot developed and deployed on azure, this bot is using Language(Custom Question Answering) service for answering users questions.
The front end part is a SPFX React WebChat which suppose to be a webpart of another sharepoint site.
The bot is working fine in Emulator or in Test WebChat(In portal) but i am facing issue about repeated welcome message when running it from spfx react webchat.
The process for React Webchat is as follow:
-
On componentDidmount() hook i request token for Direct Line :
public async componentDidMount() {
this.fetchToken();
}
private async fetchToken() {
var myToken ='<<--Secret Key-->>';
const res = await fetch('https://xxxx.azurewebsites.net/api/GetLRToken',
{ method: 'GET',
headers: {'accept': 'application/json'}
});
var { token } = await res.json();
console.log(token);
this.setState({
directLine: await createDirectLineAppServiceExtension({
domain: 'https://xxxx.azurewebsites.net/.bot/v3/directline',
token,
}),
});
this.state.directLine.postActivity({
from: { id: "serId", name: "USER_NAME" },
name: "requestWelcomeDialog",
type: "event",
value: "token"
}).subscribe(
id => console.log(`Posted activity, assigned ID ${id}`),
error => console.log(`Error posting activity ${error}`)
);
}
Which successfully fetches the token.
-
On public render(): React.ReactElement
Method i load <ReactWebChat directLine={this.state.directLine
} which is wrapped under Panel (Office UI Fabric panel)
-
In the Bot Source code on OnMembersAddedAsync method i compare added member id with turnContext recipient id as shown below and if not same i show Welcome card (Adaptive card):
protected override async Task OnMembersAddedAsync(IList membersAdded, ITurnContext turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await SendWelcomeMessageAsync(turnContext, cancellationToken);
}
}
}
This whole process and communication with bot works absolutely fine but i am unable to trace down issue why welcome screen is getting repeatedly loading our periodic time.
one observation during debugging i found is member.id is the GUID i have generated during token generation process and turncontext receipent id is something different like xxx-bot-t-xxs@kTXXB7DBjMs which seems to be a app service@some random id and i really dont know how that is being generated.
Hence it could be reason compare case always fails and shows the welcome card.
Any help to streamline the process highly appreciate, as i am new to SPFX and specially React, i dont know how to control client side code.
For reference i have attached screenshot here. Thank you in advance.
发布评论
评论(1)
根据 Microsoft 的说法,230 秒的超时是应用服务负载均衡器配置的一部分,并且无法更改。相同的超时窗口也适用于 WebSocket 连接。它适用于 HTTP 触发功能,但也适用于 Azure 应用服务。
Azure 负载均衡器的默认空闲超时设置为四分钟。此设置通常是 Web 请求的合理响应时间限制。因此,如果您的应用程序在大约 240 秒内未返回响应(Windows 应用程序为 230 秒,Linux 应用程序为 240 秒),则应用程序服务将向客户端返回超时
官方文档:为什么我的请求在 230 秒后超时?
所以下面的代码会对您有所帮助,
这基本上会在每 200 秒后发送一个空白/虚拟活动,从而防止超时。
According to Microsoft this timeout of 230 secs is part of the app service Load Balancer configuration, and it cannot be altered. The same timeout window applies for WebSocket connections as well. It is for HTTP triggered function but also applicable for Azure App Service.
Azure Load Balancer has a default idle timeout setting of four minutes. This setting is generally a reasonable response time limit for a web request. so, App Service returns a timeout to the client if your application does not return a response within approximately 240 seconds (230 seconds on Windows app, 240 seconds on Linux app)
Official Documentation : Why does my request time out after 230 seconds?
So below code will be helpful for you,
This will basically send a blank/dummy activity after every 200 seconds which prevents a timeout.