我创建了一个自定义表单流。我将把它集成到我的自定义客户端中。为此,我在 Azure 门户中创建了一个表单模板机器人并发布了我更新的代码。
我不想使用默认聊天窗口,我想集成到我的自定义聊天窗口中。为此,我设置了一个直线通道。
这是我的代码:-
public string TalkToTheBot2(string paramMessage)
{
DirectLineClient client = new DirectLineClient(DirectLineSecret);
Conversation conversation = System.Web.HttpContext.Current.Session["conversation"] as Conversation;
string watermark = System.Web.HttpContext.Current.Session["watermark"] as string;
if (conversation == null)
{
conversation = client.Conversations.StartConversation();
}
Activity message = new Activity
{
Text = paramMessage,
From = new ChannelAccount(),
Type = ActivityTypes.Message
};
var result = client.Conversations.PostActivityAsync(conversation.ConversationId, message).Result;
Chat objChat = ReadBotMessagesAsync(client, conversation.ConversationId, watermark);
System.Web.HttpContext.Current.Session["conversation"] = conversation;
System.Web.HttpContext.Current.Session["watermark"] = objChat.watermark;
objChat.ChatMessage = paramMessage;
return JsonConvert.SerializeObject(objChat);
}
private Chat ReadBotMessagesAsync(DirectLineClient client, string conversationId, string watermark)
{
Chat objChat = new Chat();
bool messageReceived = false;
while (!messageReceived)
{
var activitySet = client.Conversations.GetActivitiesAsync(conversationId, watermark).Result;
watermark = activitySet?.Watermark;
var activities = from x in activitySet.Activities
where x.From.Id == botId
select x;
foreach (Activity message in activities)
{
if (message.Text != null)
{
objChat.ChatResponse
+= " "
+ message.Text.Replace("\n\n", "<br />");
}
if (message.Attachments.Count > 0)
{
bjChat.ChatResponse
+= " " + RenderImageHTML(message.Attachments[0].ContentUrl);
}
}
messageReceived = true;
}
objChat.watermark = watermark;
return objChat;
}
在我看来,我只是将来自用户的数据发布到此操作方法,然后将操作方法的响应返回给视图。因为它是一个表单流机器人,所以很多问题将有多个选项,并且一些消息将带有附件。如何在此处管理这些选项和附件。
为了检测附件,我目前正在使用 message.Attachments.Count
.
我不知道如何管理选项。