Build command bot with JavaScript
Microsoft Teams allows you to automate repetitive tasks using a command bot. It responds to simple commands sent in chats with adaptive cards.
In this tutorial, you'll learn:
- How to build command bot with Teams Toolkit.
- About the directory structure of your app.
This step-by-step guide helps you to build command bot with Teams Toolkit. You'll see the following output:
Prerequisites
Here's a list of tools you'll need for building and deploying your apps.
| Install | For using | |
|---|---|---|
| Required | ||
| Visual Studio Code | JavaScript or TypeScript, build environments. Use latest version. | |
| Teams Toolkit | A Microsoft Visual Studio Code extension that creates a project scaffolding for your app. Use version 4.0.0. | |
| Node.js | Back-end JavaScript runtime environment. Use version 14 or 16. | |
| Microsoft Teams | Microsoft Teams to collaborate with everyone you work with through apps for chat, meetings, call and all in one place. | |
| Microsoft Edge (recommended) or Google Chrome | A browser with developer tools. | |
| Microsoft 365 developer account | Access to Teams account with the appropriate permissions to install an app. |
Note
The guide is tested on Teams Toolkit version 4.0.0 and Nodejs version 14 and 16. The steps in this guide may work with other versions, but that hasn't been tested.
Prepare development environment
After you've installed the required tools, set up the development environment.
Install the Teams Toolkit
The Teams Toolkit helps simplify the development process with tools to provision and deploy cloud resources for your app, publish to the Teams store, and more.
You can use the toolkit with Visual Studio Code or CLI (command-line interface) called TeamsFx.
Open Visual Studio Code and select Extensions (Ctrl+Shift+X / ⌘⇧-X or View > Extensions).
In the search box, enter Teams Toolkit.
Select Install next to the Teams Toolkit.
The Teams Toolkit icon appears in the Visual Studio Code sidebar after it's installed.
You can also find the Teams Toolkit on the Visual Studio Code Marketplace.
Set up your Teams development tenant
A tenant is like a space, or a container where you chat, share files, and run meetings for your organization in Teams. You can also sideload and test your app.
Check for sideloading option
After creating the app, you must load your app in Teams without distributing it. This is known as sideloading. Sign-in to your Microsoft 365 account to view this option.
Note
Sideloading is necessary for previewing and testing apps in Teams local environment. Enable Sideloading to preview and test your app in Teams locally.
Do you already have a tenant, and do you have the admin access? Let's check if you really do!
To verify sideload apps in Teams:
In the Teams client, select Store .
Select Manage your apps.
Select Publish an app.
Look for the option to Upload a custom app. If you see the option, sideloading apps is enabled.
Note
Contact Teams administrator, if you don't have the option to upload a custom app.
Create a free Teams developer tenant (optional)
If you don't have a Teams developer account, you can get it free. Join the Microsoft 365 developer program!
Go to the Microsoft 365 developer program.
Select Join Now and follow the onscreen instructions.
Select Set up E5 subscription from the welcome screen.
Set up administrator account. After you finish, the following screen displays.
Sign-in to Teams using the new administrator account. Now you should have the Upload a custom app option.
Build command bot
To build command bot using Visual Studio Code
Open Visual Studio Code.
Select the Teams Toolkit
icon in the Visual Studio Code sidebar.Select Create a new Teams app.
Select Create a new Teams app option to create an app using Teams Toolkit.
Ensure that Command bot is selected as the capability that you want to build in your app.
Select JavaScript as the programming language.
Select Default folder to store your project root folder in default location.
You can also change the default location by the following steps:
Select Browse.
Select the location for project workspace.
Select Select Folder.
Enter a suitable name for your app, such as commandbot as the application name.
Select Enter.
Note
Ensure that you use only alphanumeric characters in the application name.
By default, project opens in new window within 10 seconds. If you want to open in current window, select Open in current window.
The command bot app is created in a few seconds.
Select Run and Debug
icon from the Visual Studio Code sidebar.Select Debug(Edge) or Debug(Chrome) from the dropdown list.
Select Start Debugging button.
Select Add.
Select the command bot.
Select Enter.
You'll receive the following command response in adaptive card:
Take tour of source code
Teams Toolkit provides components for building an app. After creating the project, you can view the project folders and files in the Explorer area of Visual Studio Code.
If you choose to use UI framework, following sample template code provides a scaffolding with React components. It contains following content:
| File name | Contents |
|---|---|
bot/src/internal/initialize.js |
Create the global bot adapter and initialize with Teams Frameworks |
bot/src/index.js |
Server code to host the bot app and listen on /api/messages to process Teams activity with Bot Framework |
bot/src/helloworldCommandHandler.js |
A hello world command handler to process a helloworld command and return an adaptive card as response |
bot/src/adaptiveCards/*.json |
Adaptive card JSON file used as your command response |
Deploy your first Teams app
You've learnt to build, and run Teams app with command bot capability. The final step is to deploy your app on Azure.
Let's deploy the first app with command bot capability on Azure using Teams Toolkit.
Sign-in to your Azure account
Use your account to access the Microsoft Azure portal and to provide provision new cloud resources to support your app.
Open Visual Studio Code.
Open the project folder that you created the command bot app.
Select the Teams Toolkit
icon in the sidebar.Select Sign-in to Azure.
Tip
If you have the Azure Account extension installed and are using the same account, you can skip this step.
Your default web browser opens to let you sign-in to the account.
Sign-in to your Azure account using your credentials.
Close the browser when prompted and return to Visual Studio Code.
The ACCOUNTS section of the sidebar shows the two accounts separately. It also lists the number of usable Azure subscriptions available to you.
Note
Ensure you have at least one usable Azure subscription available.
Now you're ready to deploy your app to Azure!
Deploy your app to Azure
Under DEPLOYMENT you can provision for necessary cloud resources and your app's code is copied into the created cloud resources.
Select the Teams Toolkit
icon in the Visual Studio Code sidebar.Select Provision in the Cloud under DEPLOYMENT.
Select an existing resource group or create new resource group. For more information, see Create resource group.
Note
A dialog appears mentioning that costs may be incurred when running resources in Azure.
Select Provision.
The provisioning process creates resources in the Azure cloud. You can monitor the progress by observing the dialogs in the lower-right corner. After few minutes, you see the following notice:
You have the option to view the provisioned resources. For this tutorial, you don't need to view resources.
Select Deploy to the cloud under DEPLOYMENT after provisioning is complete.
Select Deploy.
Deployment takes some time. You can monitor the progress in the dialog in lower-right corner. After a few minutes, you see the following notice.
Open the debug panel (Ctrl+Shift+D / ⌘⇧-D or View > Run) from Visual Studio Code.
Select Launch Remote (Edge) from the launch configuration dropdown.
Select Start Debugging button.
Select Add.
Select the command bot.
Select Enter.
You'll receive the following command response in adaptive card:
Complete challenge
Did you come up with something like this?
Congratulations!
You've completed the tutorial to build command response app.
Have an issue with this section? If so, please give us some feedback so we can improve this section.
Feedback
Submit and view feedback for