使用適用於 JavaScript 的 Bot Framework SDK 建立 BotCreate a bot with the Bot Framework SDK for JavaScript

適用于: SDK v4APPLIES TO: SDK v4

本文說明如何使用 Yeoman Bot Builder 產生器和適用于 JavaScript 的 Bot Framework SDK 來建立 bot,然後如何使用 Bot Framework Emulator 進行測試。This article shows you how to build a bot by using the Yeoman Bot Builder generator and the Bot Framework SDK for JavaScript, and then how to test it with the Bot Framework Emulator.

使用 Azure Bot Service 建立 Bot 與在本機建立 Bot 是兩種不同建立 Bot 的方式。Creating a bot with Azure Bot Service and creating a bot locally are independent, parallel ways to create a bot.

必要條件Prerequisites

範本Templates

若要安裝 Yeoman 和適用于 Bot Framework v4 的 Yeoman 產生器:To install Yeoman and the Yeoman generator for Bot Framework v4:

  1. 開啟終端機或提升權限的命令提示字元。Open a terminal or elevated command prompt.

  2. 切換至您的 JavaScript Bot 目錄。Switch to the directory for your JavaScript bots. 如果您還沒有目錄,請先建立一個。Create it first if you don't already have one.

    mkdir myJsBots
    cd myJsBots
    
  3. 確定您有最新版本的 npm 和 Yeoman。Make sure you have the latest versions of npm and Yeoman.

    npm install -g npm
    npm install -g yo
    
  4. 安裝 Yeoman 產生器。Install the Yeoman generator. Yeoman 是用來建立應用程式的工具。Yeoman is a tool for creating applications. 如需詳細資訊,請參閱 yeoman.ioFor more information, see yeoman.io.

        npm install -g generator-botbuilder
    

注意

只有在您使用 Windows 作為開發作業系統時,才需要安裝以下所列的 Windows 建置工具。The install of Windows build tools listed below is only required if you use Windows as your development operating system. 在某些安裝中,restify 的安裝步驟會產生與 node-gyp 相關的錯誤。For some installations the install step for restify is giving an error related to node-gyp. 如果情況如此,您可嘗試以提升的權限執行此命令。If this is the case you can try running this command with elevated permissions. 如果您的系統上已安裝 python,此呼叫可能也會停止回應,但不會結束:This call may also hang without exiting if python is already installed on your system:

# only run this command if you are on Windows. Read the above note.
npm install -g windows-build-tools

建立 BotCreate a bot

  1. 使用產生器來建立 echo bot。Use the generator to create an echo bot.

    yo botbuilder
    

    Yeoman 會提示您輸入一些用來建立 Bot 的資訊。Yeoman prompts you for some information with which to create your bot. 本教學課程使用預設值。For this tutorial, use the default values.

    ? What's the name of your bot? my-chat-bot
    ? What will your bot do? Demonstrate the core capabilities of the Microsoft Bot Framework
    ? What programming language do you want to use? JavaScript
    ? Which template would you like to start with? Echo Bot - https://aka.ms/bot-template-echo
    ? Looking good.  Shall I go ahead and create your new bot? Yes
    

由於有範本,專案中會包含要在本快速入門建立聊天機器人所需的所有程式碼。Thanks to the template, your project contains all the code that's necessary to create the bot in this quickstart. 您不需要使用任何額外程式碼來測試您的 Bot。You don't need any additional code to test your bot.

注意

如果選擇建立 Core Bot,您需要 LUIS 語言模型。If you create a Core bot, you'll need a LUIS language model. (您可以在 luis.ai 上建立語言模型)。(You can create a language model at luis.ai). 建立模型之後,請更新設定檔。After creating the model, update the configuration file.

啟動 BotStart your bot

在終端機或命令提示字元中,將目錄變更為針對您的 Bot 所建立的目錄,並以 npm start 開頭。In a terminal or command prompt change directories to the one created for your bot, and start it with npm start.

cd my-chat-bot
npm start

此時,Bot 正在本機的連接埠 3978 上執行。At this point, your bot is running locally on port 3978.

啟動模擬器並連線至您的 BotStart the Emulator and connect your bot

  1. 啟動 Bot Framework 模擬器。Start the Bot Framework Emulator.

  2. 在模擬器的 [歡迎使用] 索引標籤上選取 [開啟 Bot ]。Select Open Bot on the Emulator's Welcome tab.

  3. 輸入 Bot 的 URL,也就是本機連接埠的 URL,並將 /api/messages 新增至路徑,通常會是 http://localhost:3978/api/messagesEnter your bot's URL, which is the URL of the local port, with /api/messages added to the path, typically http://localhost:3978/api/messages.

    開啟 bot 畫面 csopen a bot screen cs

  4. 然後選取 [連線] 。Then select Connect.

    將訊息傳送至您的 bot,bot 將會回復。Send a message to your bot, and the bot will respond back.

    執行 cs 的模擬器Emulator running cs

其他資源Additional resources

請參閱通道 (ngrok),了解如何連線到遠端裝載的 Bot。See tunneling (ngrok) for how to connect to a bot hosted remotely.

後續步驟Next steps