如何实现Microsoft Web机器人的电子邮件服务

时间:2016-08-04 19:43:51

标签: c# azure bots botframework

我正在尝试将以下几行放入我的项目中,以便测试Microsoft Bot框架的电子邮件功能:

                List<ChannelAccount> participants = new List<ChannelAccount>();
                participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));
                IMessageActivity message = Activity.CreateMessageActivity();
                bool isGroup = false;
                ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
                var ConversationId = connector.Conversations.CreateConversationAsync(cpMessage);
                //message.From = botChannelAccount;
                message.From = new ChannelAccount();
                message.Conversation = new ConversationAccount();

                message.ChannelId = "email";

                message.Text = "Hey, what's up everyone?";

                message.Locale = "en-Us";

                connector.Conversations.SendToConversation((Activity)message);
                //await connector.Conversations.SendToConversation((Activity)message);
                var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
                connector.Conversations.ReplyToActivityAsync(replyMessage);

但我没有收到任何电子邮件。

我知道&#34; print&#34;发送“哟”的声明,我听说你是肖恩&#39;当我把它放在顶部时确实有效。但它不在底部时。所以在这几行中就会出现问题。但是,当我将它推送到Azure时,我不知道如何对此进行故障排除,并且在本地进行调试并不会让我感到很困难,因为它似乎编译得很好。有没有人这样做过?

我想我已经完成了您建议的所有更新,并推送到Azure但没有生成电子邮件。这是我更新的代码:

namespace Bot_Application1
{
    [BotAuthentication]
    public class MessagesController : ApiController
    {
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                // calculate something for us to return
                int length = (activity.Text ?? string.Empty).Length;

                // return our reply to the user
                Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
                await connector.Conversations.ReplyToActivityAsync(reply);
                var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
                await connector.Conversations.ReplyToActivityAsync(replyMessage);

                // Start of my own code 
                //try { 
                // The code until the line above 
                List<ChannelAccount> participants = new List<ChannelAccount>();
                participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));

                IMessageActivity message = Activity.CreateMessageActivity();
                bool isGroup = false;
                ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
                var ConversationId = await connector.Conversations.CreateConversationAsync(cpMessage);
                message.Recipient = new ChannelAccount("shansari@verizon.net", "Shahin Ansari");
                //message.From = new ChannelAccount("shansari@verizon.net", "Shahin Ansari");

                message.From = new ChannelAccount();
                //message.From = message.ChannelData;
                message.Conversation = new ConversationAccount(id: ConversationId.Id);

                message.ChannelId = "email";

                message.Text = "Hey, what's up everyone?";

                message.Locale = "en-Us";

                connector.Conversations.SendToConversation((Activity)message);
                //await connector.Conversations.SendToConversation((Activity)message);

                //}
                //catch (Exception ex)
                //{
                //  Trace.TraceError("Sean's Exception: " + ex.ToString());
                // throw;
                //}
                //await connector.Conversations.ReplyToActivityAsync(reply);

            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;

        }

        private Activity HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                // Handle conversation state changes, like members being added and removed
                // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
                // Not available in all channels
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
            }
            else if (message.Type == ActivityTypes.Ping)
            {
            }

            return null;

        }
    }
}

1 个答案:

答案 0 :(得分:1)

主要问题是您需要在消息中设置“对话”值。也应该等待异步函数:

        List<ChannelAccount> participants = new List<ChannelAccount>();
        participants.Add(new ChannelAccount("shansari@verizon.net", "Shahin Ansari"));
        IMessageActivity message = Activity.CreateMessageActivity();
        bool isGroup = false;
        ConversationParameters cpMessage = new ConversationParameters(message.Recipient, isGroup, participants, "Quarter End Discussion");
        var ConversationId = await connector.Conversations.CreateConversationAsync(cpMessage);  
        //message.From = botChannelAccount;
        message.From = new ChannelAccount();
        message.Conversation = new ConversationAccount();

        message.ChannelId = "email";
      message.Conversation = new ConversationAccount(id: ConversationId.Id);
        message.Text = "Hey, what's up everyone?";

        message.Locale = "en-Us";

        connector.Conversations.SendToConversation((Activity)message);
        //await connector.Conversations.SendToConversation((Activity)message);
        var replyMessage = activity.CreateReply("Yo, I heard you Sean.", "en");
        await connector.Conversations.ReplyToActivityAsync(replyMessage);