Skip to content

Instantly share code, notes, and snippets.

@rainmakerho
Last active May 22, 2017 10:03
Show Gist options
  • Select an option

  • Save rainmakerho/af4ce6b32c8951115b9a176c2e1a568c to your computer and use it in GitHub Desktop.

Select an option

Save rainmakerho/af4ce6b32c8951115b9a176c2e1a568c to your computer and use it in GitHub Desktop.
新增一個 Conversation, Bot => User
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
var replyMessage = $"你輸入了 **{activity.Text}**,共輸入了**{activity.Text.Length}**個字";
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
// User的資訊是 From
var userAccount = new ChannelAccount(activity.From.Id);
// Bot 的資訊是 Recipient
var botAccount = new ChannelAccount(activity.Recipient.Id);
// 建立一個 Conversation
var replyConversation =
await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);
// 建立要回覆的 Activity
var replyMsg = Activity.CreateMessageActivity();
// 發送端是 Bot
replyMsg.From = botAccount;
// 接收端是 User
replyMsg.Recipient = userAccount;
// 對應到剛才建立的 Conversation
replyMsg.Conversation = new ConversationAccount(id: replyConversation.Id);
// 設定回覆的訊息字串
replyMsg.Text = replyMessage;
// 透過 ConnectorClient 專訊息傳送給 User
await connector.Conversations.SendToConversationAsync((Activity)replyMsg);
}
else
{
await HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment