Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 会话不会恢复bot框架_C#_Facebook_Botframework - Fatal编程技术网

C# 会话不会恢复bot框架

C# 会话不会恢复bot框架,c#,facebook,botframework,C#,Facebook,Botframework,我正在使用Botframework在C#中构建一个facebook登录对话框,但在获得访问令牌后,它通常不会恢复对话,我不明白为什么。我遵循以下示例:但我无法继续我的对话,我得到以下例外: System.InvalidOperationException异常 消息:由于对象的当前状态,操作无效 它发生在我想继续对话的时候 [HttpGet] [Route("api/OAuthCallback")] public async Task<HttpResponseMessage&

我正在使用Botframework在C#中构建一个facebook登录对话框,但在获得访问令牌后,它通常不会恢复对话,我不明白为什么。我遵循以下示例:但我无法继续我的对话,我得到以下例外:

System.InvalidOperationException异常

消息:由于对象的当前状态,操作无效

它发生在我想继续对话的时候

[HttpGet]
    [Route("api/OAuthCallback")]
    public async Task<HttpResponseMessage> OAuthCallback([FromUri] string userId, [FromUri] string botId, [FromUri] string conversationId, [FromUri] string channelId, [FromUri] string serviceUrl, [FromUri] string locale, [FromUri] string code, [FromUri] string state, CancellationToken token)
    {
        // Get the resumption cookie
        var address = new Address
            (
                botId: FacebookHelper.TokenDecoder(botId),
                channelId: channelId,
                userId: FacebookHelper.TokenDecoder(userId),
                conversationId: FacebookHelper.TokenDecoder(conversationId),
                serviceUrl: FacebookHelper.TokenDecoder(serviceUrl)
            );
        var resumptionCookie = new ResumptionCookie(address, userName: null, isGroup: false, locale: locale);

        var accessToken = await FacebookHelper.ExchangeCodeForAccessToken(resumptionCookie, code, FacebookAuthDialog.FacebookOauthCallback.ToString());

        // Create the message that is send to conversation to resume the login flow
        var msg = resumptionCookie.GetMessage();
        msg.Text = $"token:{accessToken.AccessToken}";

        // Resume the conversation to FacebookAuthDialog
[HttpGet]
[路由(“api/OAuthCallback”)]
公共异步任务OAuthCallback([FromUri]字符串用户ID、[FromUri]字符串botId、[FromUri]字符串会话ID、[FromUri]字符串通道ID、[FromUri]字符串服务URL、[FromUri]字符串语言环境、[FromUri]字符串代码、[FromUri]字符串状态、取消令牌)
{
//拿到饼干
var地址=新地址
(
botId:FacebookHelper.TokenDecoder(botId),
channelId:channelId,
userId:FacebookHelper.TokenDecoder(userId),
conversationId:FacebookHelper.TokenDecoder(conversationId),
serviceUrl:FacebookHelper.TokenDecoder(serviceUrl)
);
var resumptionCookie=new resumptionCookie(地址、用户名:null、isGroup:false、区域设置:区域设置);
var accessToken=等待FacebookHelper.ExchangeCodeForAccessToken(resumptionCookie,代码,FacebookAuthDialog.FacebookOauthCallback.ToString());
//创建发送到对话的消息以恢复登录流
var msg=resumptionCookie.GetMessage();
msg.Text=$“令牌:{accessToken.accessToken}”;
//将对话恢复到FacebookAuthDialog
//我在哪里得到了例外

wait Conversation.ResumeAsync(resumptionCookie,msg)

使用(var scope=DialogModule.BeginLifetimeScope(Conversation.Container,msg))
{
var-dataBag=scope.Resolve();
等待dataBag.LoadAsync(令牌);
待恢复;
if(dataBag.PrivateConversationData.TryGetValue(“persistedCookie”,out挂起))
{
//删除持久化cookie
dataBag.PrivateConversationData.RemoveValue(“persistedCookie”);
等待dataBag.FlushAsync(令牌);
return Request.CreateResponse(“您现在已登录!继续与bot对话”);
}
其他的
{
//在没有挂起消息的情况下调用回调,因此无法恢复登录流。
返回请求.CreateErrorResponse(HttpStatusCode.BadRequest,新的InvalidOperationException(“无法恢复”);
}
}
}

此OAuth方法适用于较旧版本的bot框架。对于新版本的bot框架来说,更好的解决方案是可行的

        using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, msg))
        {
            var dataBag = scope.Resolve<IBotData>();
            await dataBag.LoadAsync(token);
            ResumptionCookie pending;
            if (dataBag.PrivateConversationData.TryGetValue("persistedCookie", out pending))
            {
                // remove persisted cookie
                dataBag.PrivateConversationData.RemoveValue("persistedCookie");
                await dataBag.FlushAsync(token);
                return Request.CreateResponse("You are now logged in! Continue talking to the bot.");
            }
            else
            {
                // Callback is called with no pending message as a result the login flow cannot be resumed.
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new InvalidOperationException("Cannot resume!"));
            }
        }
    }