我试图在webapi中为我的Microsoft bot实现自定义连接器,以便可以通过Rest API从不同的应用程序调用它(例如:来自rasberripy)。目的是在里面实现自定义连接器一个使用直接API的WebAPI,这样其他应用程序就可以使用rest调用来与这个API进行通信,然后使用dirtectline API与我的bot进行对话。我测试了以下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和user-question调用Bot / ReadBotMessages。
我面临的问题是代码
var activitySet = client.Conversations.GetActivities(conversationId, watermark);
每次被击中时都会返回多个活动,也可以回复之前的问题(),但是我应该得到与当前响应相关的单个活动,同样的逻辑工作正常来自github的控制台应用程序,它只返回一个当前活动。任何人都可以建议一个解决方法,或者我在做什么工作?
答案 0 :(得分:1)
为了获得最近的活动,您需要确保将当前水印存储在连接器中的某个位置。
调用以下方法:
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}
从&#34; watermark&#34;返回机器人存储的对话。或者,如果设置为null,则机器人内存中可用的对话部分从机器人reconnecting之前发送的机器人发送的最后一条消息开始(这是您所看到的)。