Office 365 Robot (Bot) Development Beginner's Guide (new)

Office 365 Robot (Bot) Development Beginner's Guide (new)

This article is reprinted: Author Chen Xizhang, http://chenxizhang.cnblogs.com/p/8453915.html?from=timeline&isappinstalled=0

When I was sorting out the manuscript recently, I found that I wrote this introduction to Office 365 robot (Bot) development in July 2017.   Because of the upgrade of the relevant platform, it can no longer reflect the current development process, so I will write another article. The new development getting started guide is for everyone who is interested.

I don't know the exact start time of this platform upgrade, but if you continue to visit  https://dev.botframework.com/ now  , you will see a clear prompt that requires the previously created Bot to be released in 2018 Migrate to the new Azure Bot Service by March 31st:

If you clicked the "Create a bot" button at this point, you will also be asked to do so through the Azure Portal:

This will mean that to develop bots now, you must first have an Azure subscription, either a trial version or a production version. This article will assume that you already have these conditions.

It is worth noting that the current domestic version of Azure does not have the function of Bot Service.

Three different types of Bots

You can search for Bot Service in the Azure Portal, or quickly locate the three currently supported Bot Service types through  this address  , as shown in the following figure:

Their respective usage scenarios are as follows

  1. Web App Bot. This type will create an App Service in Azure to run your bot and greatly simplify your development process with templates and automated configuration.
  2. Function Bot. This type will create an Azure Function App in Azure to run your Bot, and there will also be templates and automated configuration to simplify development. The difference between it and Web App Bot is that its billing is based on specific usage times. Not when the virtual machine is activated - in fact, this is the essential difference between Azure Function App and Web App. I personally think that this form should be more in line with the characteristics of robots - it is called on demand and does not necessarily have to run in the background all the time.
  3. Bot Channels Registration. This type allows you to deploy the Bot application to other locations of your choice (maybe your data center or other cloud platforms), and then register and connect to the Channel through Azure. If you have read my last article, you should have some concepts about Bot, Bot Framework, and Channel. If you still don't understand, I will continue to do some explanations.

In the development stage, no matter which type of Bot is mentioned above, we can choose a "free" price for development and debugging (unlimited messages for ordinary channels, 10,000 message calls per month for advanced channels). A "free" service is not guaranteed by an SLA, but is sufficient for the development phase.

Three Common Azure Bot Service Scenarios

Although we know that creating a Bot is not difficult, the second half of this article will once again use an example to introduce how to develop and test a bot based on the Azure Bot Service. But before that, I still extract three common Azure robot service solutions for your reference. Understanding these popular practices and processes in the industry may be useful for your subsequent development.

business chatbot

The combination of Azure Bot Service and Language Understanding Service enables developers to create conversational interfaces for various scenarios, such as banking, travel, and entertainment. For example, hotel concierges can use bots to augment traditional email and phone call interactions by authenticating customers through Azure Active Directory and use Cognitive Services to better contextually process customer requests with text and voice. A speech recognition service can be added to support voice commands.

Information Chatbot

此信息机器人可回答知识集中定义的问题或使用认知服务 QnA Maker 回答常见问题,以及使用 Azure 搜索回答更加开放的问题。

企业效率聊天机器人

Azure 机器人服务可轻松与语言理解结合以生成强大的企业效率机器人,让组织可以通过集成外部系统(如 Office 365 日历、Dynamics CRM 中存储的客户事例等)来简化常见工作活动。

Function Bot 开发和调试

下面我将以一个实例来演示如何开发和调试Function Bot。在下图的向导中,你需要指定一个唯一的名称,并且选择存储位置,定价层(我选择F0,是指免费的定价),宿主计划我选择的是“消耗计划”指的是按调用次数付费,Application insights选择“打开”以便后期可以通过一个仪表盘来看到机器人被调用的统计数据。

你已经看到了,Azure Bot Service默认提供了两种语言(C#和Node.js)的五种模板。我先以Basic为例创建一个应用。创建成功后,请在下面的界面中点击“Test in Web Chat”来进行测试。

在线修改代码并且进行测试

这就是Basic模板默认提供的功能,它就像是一个回声筒一样,将你发送过去的话再返回过来。如果你觉得这样太无聊了,你当然可以修改代码让它变得有趣一些。请点击“机器人管理”中的“内部版本”这个菜单。

点击“在Azure Functions中打开此机器人”链接,在接下来的界面中,找到EchoDialog.csx这个文件,按照下面红色框示意修改代码

点击“保存”按钮,然后回到此前的"Test in Web Chat”页面,再次输入你的消息,观察其返回的内容,现在在回复消息中多了一个时间戳了。

本地修改机器人代码并实现持续整合

以上演示了如何在线修改代码并进行测试的方法。只要你愿意,你随时可以将代码下载到本地,然后使用你喜欢的编辑器进行本地开发,最后提交给Azure Bot Service。请在下图中点击“下载zip文件”链接。

你需要使用Visual Studio 2017打开这个解决方案文件

将上面这一行代码稍作修改,例如:await context.PostAsync($"{this.count++}: You said {message.Text} at {DateTime.Now},Modify by Visual Studio");

接下来,我们要将本地这个目录进行git配置,以便后续可以跟Azure Bot Service 进行持续整合(通过git的代码提交,自动替换Azure Bot Serivce代码并触发编译,更新Bot应用)。请确保你的本地计算机上面安装了git工具。

以上通过git init命令初始化当前目录的git仓库。然后通过git add .命令和git commit -m命令提交本地更新。接下来我们配置Bot Service以便它能使用本地git仓库进行持续整合。

请点击上图的“所有应用服务设置”菜单,并且接下来的“部署选项”中选择“本地Git存储库”选项

点击“保存”按钮后,设置“部署凭据”。请牢记这个用户名和密码,并且不要泄露给其他人。

在“概述”页面中,此时会多出来一个Git的克隆Url,如下图所示

请将这个地址复制下来,接下来回到git bash的窗口。通过git remote add origin 你的url 命令添加远程存储库绑定,并且通过git push origin master命令来完成代码推送。

推送成功后稍等片刻,再次回到Azure Bot Service的“Test in Web Chat”菜单,你会发现刚才我们在Visual Studio中进行的代码修改已经起了作用,如下图所示。

使用Bot Framework Emulator进行调试

如果你想进行更加细节的调试,我推荐你下载和安装 Bot Framework Emulator。通过它来进行调试的好处是可以清晰地看到消息发送和接收的细节,如下图所示

在你的业务应用中整合这个机器人

上面我们演示了如何开发、测试和调试机器人,默认情况下,Azure Bot Service会将这个机器人连接到一个Web Chat的信道(Channel),这样的话,我们既可以通过之前多次演示的“Test in web chat”界面进行使用,但也可以将这个界面整合到自己的业务应用中来,为此我们需要获取机器人嵌入代码,如下图所示

你可以配置多个站点,并且为每个站点都生成一个单独的密钥以进行区分,然后点击“复制”按钮,实际上你会得到一串HTML代码,里面是一个iframe。请注意用你的密钥替换掉代码中的“使用此处的密钥”,请将代码保存为一个HTML文件,如下图所示

请注意,我这里添加了一个Style的设置,这是为了让它在浏览器中看起来更加美观一些。接下来你可以在任意浏览器中打开这个本地网页,输入消息后你会得到跟此前一致的使用体验。

将机器人连接到Microsoft Teams

既然这篇文章讲的是“Office 365 机器人(Bot)开发入门”,自然要提到如何跟Office 365的结合。这个话题有两层含义,首先在Bot Service中可以通过Microsoft Graph调用Office 365的服务来完成一些工作,其次是我们可以将机器人连接到Office 365的组件中来,目前支持Microsoft Teams和Skype for Business两个信道,如下图所示

添加到Microsoft Teams相对容易一些,你只需要点击上图中的Microsoft Teams图标,并且接受协议,在下图中点击“完成”即可。

回到信道主界面,点击“Microsoft Teams”的链接,即可为自己的Microsoft Teams客户端添加当前这个机器人。

如果不出意外的话,你的联系人中会出现一个机器人,你可以像跟同事聊天一样与它进行互动了。

如果你的同事也需要使用这个机器人,在你没有将这个应用提交给微软官方的市场之前,他们需要通过机器人的编号进行搜索,如下图所示

添加联系人后,后续的聊天形式是一样的

关于如何将你开发的这个机器人提交到微软的官方市场,请参考 https://docs.microsoft.com/zh-cn/microsoftteams/platform/publishing/apps-publish 的说明。

将机器人连接到Skype for Business

与Microsoft Teams相比,将机器人连接到Skype for Business的体验正好相反——它的安装配置过程比较复杂(需要Office 365管理员权限),但一旦配置完成,则整个公司的用户都能直接搜索到这个机器人,而无需发布到微软的应用市场。

添加Skype for Business这个信道只是第一步,接下来要根据一个文档的说明,使用Office 365管理员身份以及几个PowerShell的命令来完成这个机器人的注册和配置。通常的指令形式如下图所示

在我的Office 365测试环境中,我执行的命令如下图所示(请注意,第二个命令的执行可能需要几分钟时间)

完成上面的配置后,任何一个用户,都可以直接在Skype for Business中搜索中这个机器人并且跟它聊天了。

请注意,我前面已经提到了,因为当前这个机器人是托管在免费的模式下,所以可能你在测试过程中偶尔会遇到一些错误。

结语

新年新气象,这篇文章给大家完整地介绍了全新的Azure Bot Service提供的服务。我们可以利用它快速地完成机器人的开发、测试,根据自己的需要将其连接到包括Microsoft Teams和Skype for Business在内的多个信道中去,真正实现“一次编写、处处运行”,我相信这种平台级别的创新,结合Office 365的庞大用户群体,以及海量的有价值的信息,使得机器人(Bot)不再是一个实验室中的花骨朵,而是我们现实工作和生活中可以触手可及的应用。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325485417&siteId=291194637