gpt4 book ai didi

c# - Bot 框架实现自定义连接器 "client.Conversations.GetActivities(conversationId, watermark)"返回多个事件

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:50 24 4
gpt4 key购买 nike

我正在尝试在 webapi 中为我的 Microsoft 机器人实现自定义连接器,以便可以通过 Rest API 从不同的应用程序调用它(例如:来自 rasberripy)。目的是在 WebAPI 中实现自定义连接器使用 directline API,这样其他应用程序就可以使用 rest 调用来与这个 API 对话,而这个 API 又会使用 dirtectline API 与我的机器人对话。我测试了以下 code from micrososft它在控制台应用程序中工作。我已将相同的代码移至 WebAPI,此处相同的代码表现不同。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using Microsoft.Bot.Connector.DirectLine;
using Newtonsoft.Json;
using System.Configuration;

namespace BotConnector.Web.Controllers
{
public class BotController : Controller
{

private static string directLineSecret = ConfigurationManager.AppSettings["DirectLineSecret"];
private static string botId = ConfigurationManager.AppSettings["BotId"];
private static string fromUser = "APISampleClientUser";

// GET: Bot
public ActionResult Index()
{
return View();
}

public string Connect()
{
var conversation=StartBotConversation();
return conversation.ConversationId;
}

public string ReadBotMessages(string conversationId,string message)
{

//DirectLineClient client = (Session["client"] == null) ? new DirectLineClient(directLineSecret) : Session["client"] as DirectLineClient;
//Session["client"] = client;

DirectLineClient client = new DirectLineClient(directLineSecret);

var activity = new Activity
{
From = new ChannelAccount(fromUser),
Text = message,
Type = ActivityTypes.Message
};

client.Conversations.PostActivityAsync(conversationId, activity);
var response = ReadBotMessagesAsync(client, conversationId);

//var heroCard = JsonConvert.DeserializeObject<HeroCard>(response[0].Content.ToString());
return response.Result;
}

private static Conversation StartBotConversation()
{
//var session = System.Web.HttpContext.Current.Session["client"];
//DirectLineClient client = (session == null) ? new DirectLineClient(directLineSecret) : session as DirectLineClient;
//System.Web.HttpContext.Current.Session["client"] = client;
var client = new DirectLineClient(directLineSecret);

var conversation = client.Conversations.StartConversationAsync().Result;

//new System.Threading.Thread(async () => await ReadBotMessagesAsync(client, conversation.ConversationId)).Start();

return conversation;
}


private static async Task<string> ReadBotMessagesAsync(DirectLineClient client, string conversationId)
{
string watermark = null;
var attachments = new List<Attachment>();
string message = string.Empty;

while (true)
{
var activitySet = client.Conversations.GetActivities(conversationId, watermark);
watermark = activitySet?.Watermark;

var activities = from x in activitySet.Activities
where x.From.Id == botId
select x;

foreach (Activity activity in activities)
{
message = activity.Text;

if (activity.Attachments != null)
{
foreach (Attachment attachment in activity.Attachments)
{
if (attachment.ContentType== "application/vnd.microsoft.card.hero")
{
var heroCard = JsonConvert.DeserializeObject<HeroCard>(attachment.Content.ToString());
message = heroCard.Text;
//attachments.Add(attachment);
}
}
}
}
//if (attachments.Count > 0) break;
if (!string.IsNullOrEmpty(message)) break;


//await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
}

return message;
}

}

首先,我将调用 Bot/Connect 来生成 conversationId,稍后我将使用 ID 和用户问题调用 Bot/ReadBotMessages。

我面临的问题是代码

var activitySet = client.Conversations.GetActivities(conversationId, watermark); 每次被击中时返回多个事件,以及之前问题的回复(Image here),但我应该得到与当前响应相关的单个事件,相同的逻辑在 github 的控制台应用程序中运行良好它只返回一个当前事件。任何人都可以提出解决方法或者我在做什么吗?

最佳答案

为了只获取最近的事件,您需要确保将当前水印存储在连接器中的某个位置。

调用以下方法:

Client.Conversations.PostActivityAsync(conversationId, activity);

将返回一个格式如下的字符串:

"IRBO9ZhxkwID1QnNbAoOHX|0000008"

"{ConversationId|Watermark}"

您需要将此调用的水印部分保存在您的连接器中的某个位置,因为稍后在您的连接器的消息接收调用中使用它将允许您仅获得对消息的即时响应:

    private static async Task<string> ReadBotMessagesAsync(DirectLineClient client, string conversationId)
{
string watermark = null; //this should be the current watermark

基本上,对话 GET 调用的端点:

/v3/directline/conversations/{conversationID}/activities?watermark={watermark}

返回机器人存储的从“水印”开始的对话,或者,如果它设置为 null,则返回机器人内存中从机器人在 reconnecting 之前发送的最后一条消息开始的可用对话部分。与机器人(这就是您所看到的)。

关于c# - Bot 框架实现自定义连接器 "client.Conversations.GetActivities(conversationId, watermark)"返回多个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50703476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com