Build notification bot with JavaScript
The notification bot proactively sends messages in Teams channel or group chat or personal chat. You can trigger the notification bot with an HTTP request, such as cards or texts.
In this tutorial, you learn:
- How to create a new notification bot with Teams Toolkit.
- About the directory structure of your app.
- How to send multiple notifications.
This step-by-step guide helps you to build notification bot with Teams Toolkit. You can see the following output:
Prerequisites
Here's a list of tools you need for building and deploying your apps.
| Install | For using | |
|---|---|---|
| Visual Studio Code | JavaScript, or TypeScript, build environments. Use the 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. For more information, see Node.js version compatibility table for project type. | |
| Microsoft Teams | Microsoft Teams to collaborate with everyone you work with through apps for chat, meeting, call, and all in one place. | |
| Microsoft Edge (recommended) or Google Chrome | A browser with developer tools. | |
| Microsoft 365 | 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 Activity Bar after it's installed.
You can also find the Teams Toolkit on the Visual Studio Code Marketplace.
Note
The latest version of Teams Toolkit is 4.2.0.
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 the 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:
Go to Microsoft Teams.
Select Apps > Manage your apps.
Select Upload 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 see 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 notification bot
To build notification bot using Visual Studio Code
Open Visual Studio Code.
Select the Teams Toolkit
icon in the Visual Studio Code Activity Bar.Select Create a new app.
Select Create a new Teams app option to create an app using Teams Toolkit.
Ensure that Notification Bot is selected as the capability that you want to build in your app.
Select HTTP Trigger Restify Server as the trigger.
Select OK.
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 the Select Folder.
Enter a suitable name for your app.
Select Enter.
After your app is created, the Teams Toolkit displays the following message:
The notification bot app is created in a few seconds.
Select Run and Debug
icon from the Visual Studio Code Activity Bar.Select Debug(Edge) or Debug(Chrome) from the dropdown list.
Select Start Debugging button.
Select Add.
Open Windows PowerShell.
Run the
Invoke-Webrequest -Method POST -URI http://localhost:3978/api/notificationcommand.
You get the following output in Teams:
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.
The new project folder contains following content:
| Folder name | Contents |
|---|---|
.fx |
Project level settings, configuration, and environment information |
.vscode |
VS code files for local debug |
bot |
The bot source code |
templates |
Templates for Teams app manifest and corresponding Azure resources |
The core notification implementation in bot folder and it contains:
| File name | Contents |
|---|---|
src/adaptiveCards/ |
Templates for Adaptive card |
src/internal/ |
Generated initialize code for notification functionality |
src/index.*s |
The entrypoint to handle bot messages and send notifications |
.gitignore |
File to exclude local files from bot project |
package.json |
The npm package file for bot project |
README.md |
README.md file is to know more about the notification bot |
Send notification to Teams channel
To send notification to Teams channel
In Visual Studio Code, under EXPLORER, select MYNOTIFICATIONBOT> bot> src> index.js.
You can see the following page:
Use the following code after
for (const target of await bot.notification.installations()) {in Visual Studio Code.if (target.type === "Channel") { const members = await target.members(); await target.sendMessage("This is a message to channel: " + members.length + " members."); for (const member of members) { await member.sendMessage("This is a message to Member: " + member.account.email); } } /*
To customize storage:
In Visual Studio Code, under EXPLORER, select MYNOTIFICATIONBOT> bot> src> internal> initialize.js.
You can see the following page:
Update the following code in initialize.js file to customize the storage.
const myStorage = new MyStorage(...); // initialize ConversationBot with notification enabled and customized storage const bot = new ConversationBot({ // The bot id and password to create BotFrameworkAdapter. // See https://aka.ms/about-bot-adapter to learn more about adapters. adapterConfig: { appId: process.env.BOT_ID, appPassword: process.env.BOT_PASSWORD, }, // Enable notification notification: { enabled: true, storage: myStorage, }, });Use your own storage location instead of MyStorage.
Note
It's required to use your own storage for production environment. If storage isn't provided, default local file storage is considered.
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.
You get the following output in Teams.
Select Add to a team in the dropdown list.
In Search box, add any channel.
Select Set up a bot.
The following screen appears:
Open Windows PowerShell.
Run the
Invoke-Webrequest -Method POST -URI http://localhost:3978/api/notificationcommand.
You get the notification in the Teams channel:
You get the notification in the personal chat:
Deploy your first Teams app
You've learned to build and run Teams app with notification bot capability. The final step is to deploy your app to Azure.
Let's deploy the first app with notification 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 where you created the notification 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.
Select anyone of the existing subscription.
Select an existing or create new 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 Azure cloud. You can monitor the progress by observing the dialogs in the lower-right corner.
You see 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 dialogs in lower-right corner.
Complete challenge
Did you come up with something like this?
Congratulations!
You've completed the tutorial to build a notification bot. You can extend your notification bot to add the command response and workflow features.
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