我创建了一个自定义的表单流程。我将把它集成到我的自定义客户端。为此,我在Azure门户中创建了一个表单模板bot,并发布了我的更新代码。
我不想使用默认聊天窗口,我想要集成到我的自定义聊天窗口中。为此,我建立了一个直线渠道。
这是我的代码: -
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
。
我不知道如何管理选项。