我是微軟機器人的新手。 我的公司有自己的通信應用程序,我想將我的機器人與通信應用程序連接起來,我的客戶可以在我的campany的通信應用程序上使用我的機器人。我讀過它需要使用Direct Line來做到這一點。但我真的不知道該怎麼做。 有人幫我嗎?還是給我一些建議?或者其中的任何例子。 非常感謝。微軟機器人與直線休息API爲新手的BOT
回答
請參考documentation關於Bot框架的直線方法。
您需要做的是使用https://directline.botframework.com/api/conversations作爲您的端點,並按文檔中所示調用這些API。
例如: - 我剛剛嘗試使用ASP.MVC應用程序。我創建了一個文本框和按鈕來提交消息給機器人。
- 首先啓用您的機器人應用程序的直接鏈接。然後記住那個祕密。
以下代碼示例說明如何將您的聊天應用程序或公司應用程序與使用機器人框架構建的機器人連接起來。
首先,您需要授權您訪問直接鏈接API。
client = new HttpClient(); client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "[Your Secret Key Here]");
response = await client.GetAsync(「/ api/tokens /」);
如果(response.IsSuccessStatusCode)
如果你是一個響應的成功,你就可以開始一個新的對話 型號 -
公共類談話 { 公共字符串的conversationId {獲得;組; } public string token {get;組; } public string eTag {get;組; }}
代碼裏面控制器 -
var conversation = new Conversation();
response = await client.PostAsJsonAsync("/api/conversations/",conversation);
if (response.IsSuccessStatusCode)
如果你有這種反應的成功,你將得到的conversationId和令牌啓動的消息。
然後通過下面的代碼通過您的消息發送到機器人,
會話ConversationInfo = response.Content.ReadAsAsync(typeof運算(對話))的結果作爲會話。 string conversationUrl = ConversationInfo.conversationId +「/ messages /」; 消息msg = new Message(){text = message}; response = await client.PostAsJsonAsync(conversationUrl,msg); 如果(response.IsSuccessStatusCode)
爲了從機器人消息,
響應=等待client.GetAsync(conversationUrl); (Response.IsSuccessStatusCode)MessageSet BotMessage = response.Content.ReadAsAsync(typeof(MessageSet))。Result as MessageSet; ViewBag.Messages = BotMessage; IsReplyReceived = true; }
如果你得到一個成功的響應,這意味着您已發送郵件到機器人。現在需要從BOT得到的回覆消息
在這裏你得到一個消息集,這意味着你發送的消息和Bot的回覆。您現在可以在聊天窗口中顯示它。
消息模型 -
public class MessageSet
{
public Message[] messages { get; set; }
public string watermark { get; set; }
public string eTag { get; set; }
}
public class Message
{
public string id { get; set; }
public string conversationId { get; set; }
public DateTime created { get; set; }
public string from { get; set; }
public string text { get; set; }
public string channelData { get; set; }
public string[] images { get; set; }
public Attachment[] attachments { get; set; }
public string eTag { get; set; }
}
public class Attachment
{
public string url { get; set; }
public string contentType { get; set; }
}
使用這些API調用,您可以輕鬆地與機器人框架連接任何的自定義聊天應用。以下是一種方法中的完整代碼,供您瞭解如何歸檔目標。
private async Task<bool> PostMessage(string message)
{
bool IsReplyReceived = false;
client = new HttpClient();
client.BaseAddress = new Uri("https://directline.botframework.com/api/conversations/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "[Your Secret Code Here]");
response = await client.GetAsync("/api/tokens/");
if (response.IsSuccessStatusCode)
{
var conversation = new Conversation();
response = await client.PostAsJsonAsync("/api/conversations/", conversation);
if (response.IsSuccessStatusCode)
{
Conversation ConversationInfo = response.Content.ReadAsAsync(typeof(Conversation)).Result as Conversation;
string conversationUrl = ConversationInfo.conversationId+"/messages/";
Message msg = new Message() { text = message };
response = await client.PostAsJsonAsync(conversationUrl,msg);
if (response.IsSuccessStatusCode)
{
response = await client.GetAsync(conversationUrl);
if (response.IsSuccessStatusCode)
{
MessageSet BotMessage = response.Content.ReadAsAsync(typeof(MessageSet)).Result as MessageSet;
ViewBag.Messages = BotMessage;
IsReplyReceived = true;
}
}
}
}
return IsReplyReceived;
}
感謝與你一起幹杯。
- 1. 開始與Microsoft bot構建器和微軟機器人框架的對話
- 2. C#,微軟機器人
- 3. 微軟機器人和SQL
- 4. BotKit Slack Bot到微軟團隊機器人
- 5. 微軟Bot Builder與.NET 4.5?
- 6. 主動消息與提示按鈕 - 微軟機器人框架
- 7. 發佈一個微軟機器人
- 8. 微軟機器人工作室
- 9. 在微軟機器人框架
- 10. 微軟機器人透明認證
- 11. 如何開發與微軟機器人SDK和LUIS聊天機器人conected
- 12. 微軟BotFramework。如何獲取機器人信息(例如圖標)
- 13. 微軟DirectLine API到機器人不起作用
- 14. 新手微軟Azure問題
- 15. System.Net.Http.HttpRequestException(微軟Bot框架)
- 16. 微軟bot框架模擬器不能脫機工作
- 17. 如何將我的python機器人連接到微軟機器人連接器
- 18. 微軟楔形手機鍵盤
- 19. 直線API微軟Bot框架 - 獲取令牌,而不公開祕密的令牌
- 20. 微軟Bot框架。隱藏來自用戶的文本消息
- 21. 微軟圖形API:@個人賬戶
- 22. 紗線得到休息API
- 23. 微軟面對API - 爲同一人faceId值是與每個API調用
- 24. 微軟的Outlook API
- 25. 無法通過REST API發送消息給Facebook機器人Bot Botworkwork
- 26. 微軟機器人主動消息 - 不能轉換從組串 - IMessageActivity字符串
- 27. 微軟機器人工作室和絕對路徑的問題
- 28. 發送附件nodejs的微軟機器人框架
- 29. 機器人不回覆微軟團隊中的郵件
- 30. null botId - 微軟Bot框架SDK V3
你好,如何使相同的代碼的android? –
你現在可以使用DirectLine SDK :) https://www.nuget.org/packages/Microsoft.Bot.Connector.DirectLine/ – Raph