BotFramework学习-01

微软在Build2016大会上表示,未来将是一个充满聊天机器人的世界,为此他们推出了微软Bot Framework,能够允许任何人制作自己的聊天机器人,微软则提供“cognitive microservices”(认知微服务),该工具能够理解自然语言或者对图片进行分析,初期开放22个API,可用于集成到应用中。

微软Bot Framework允许开发者将自己开发的机器人集成到一系列平台中,例如Skype、Slack、Telegram、电子邮件和网页等,微软称希望所有的开发者能够将自己的应用变得更加智能。

打算将BotFramework的学习过程记录下来。

1、安装需求

  • vs2015(update2以上)
  • 更新vs到最新版本

2、下载安装BotFramework模板

  • 下载模板文件(下载
  • 保存下载的zip模板文件到vs2015的模板目录下(%USERPROFILE%\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#)

3、打开vs2015,使用Bot Application 模板创建一个C#项目

 

4、代码

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

                // return our reply to the user
                return message.CreateReplyMessage($"You sent {length} characters");
            }
            else
            {
                return HandleSystemMessage(message);
            }
        }

        private Message HandleSystemMessage(Message message)
        {
            if (message.Type == "Ping")
            {
                Message reply = message.CreateReplyMessage();
                reply.Type = "Ping";
                return reply;
            }
            else if (message.Type == "DeleteUserData")
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == "BotAddedToConversation")
            {
            }
            else if (message.Type == "BotRemovedFromConversation")
            {
            }
            else if (message.Type == "UserAddedToConversation")
            {
            }
            else if (message.Type == "UserRemovedFromConversation")
            {
            }
            else if (message.Type == "EndOfConversation")
            {
            }

            return null;
        }
    }
View Code

 

5、调试

首先下载bot framework 模拟器测试bot应用程序

1、下载地址(bot emulator

 

posted @ 2016-04-25 12:39  vitas2015  阅读(766)  评论(0编辑  收藏  举报