Skip to content

A teams app bot backend created with help of express, helps us to easily setup more than one bots without using azure functions.

Notifications You must be signed in to change notification settings

Aman-Dhyani/multiple-teams-bot-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Set Up Your Teams "App" with this "Bot" Backend template – Without using Azure Functions

Features

  1. handling instatllation
  2. handling message/commnds reply
  3. handling Context/interaction
  4. sending Adaptive card
  5. updating Adaptive card
  6. Toggling modal/dialog
  7. handle multiple bots (add as many in .env file)

Working

1. installing/adding bot (handleInstallation)

  • bot will welcome you

    image

  • also show your email & bot converationID on console. (which you can customize to use later)

    image

    e.g. you can store (userEmail, conversationID) of whoever install your bot, then later you can send them messages through bot by using Bot's conversationID based on their email. like sending new update or specific message to your community😊.

  • welcome adaptive card will contain 2 button "hi" and "hello"

    image

2. invoke/interaction with bot (handleContext/handleInteraction)

  • clicking on "hi" and "hello" from welcome message will open a modal/dialog contains bot commands.

    for hi ("bot -greet hi")

    image

    for hello ("bot -greet hello")

    image

3. messageing to bot (handleCommand)

  • message those commands to bot "bot -greet hello"

    image

  • bot will response with greet and a feedback btn

    image

  • click will open modal/dialog like this

    image

  • clicking submit will close modal and update that adaptive card which you use earlier to open this modal/dialog

    image

How to Setup

6 Simple Steps to setup this template as your teams Bot backend logic :-

Step 1 :-

  • Having app in MS-teams.

    image

Step 2 :-

  • Having a Bot also MS-teams.

    image

Step 3 :-

  • Copy Name, App ID and Client secret of the bot.

  • paste those details in .env file.

    image

note:- you can put anything as bot route keep in mind later, that bot route will define specific dynamic endpoint to you bot.

image

"http://localhost:3002/template/api/messages/whateverBotRoutOfThatBot"

Step 4 :- Now,

  • run this template over https (bcs azure/teams support bot to run only for ssl), follow these steps.

  • npm install (all dependencies) In my case I am using pm2 and nginx. after doing

    • pm2 start index.js,
    • service nginx start,
    • pm2 logs

    you will see this.

    image

  • copy "https://yourdomain.com/template/api/messages/whateverBotRoutOfThatBot"

  • paste it to bots endpoint configuration

for azure

![image](https://github.com/user-attachments/assets/8c5205b1-4bbe-4daa-97c9-97eaf8b7ce2f)

for teams Dev portal

![image](https://github.com/user-attachments/assets/a49ff7ea-6f8b-4d0b-8f74-64a817ad490f)

step 5 :-

Connect your app with you bot

  • visit teams app dev portal

  • visit your app

    image

  • open App Features tab and click on Bot

    image

  • choose your bot among other bots

    image

  • select scope also (personal is good for me, so we dont have to install bot every time for each team channels or group

    image

  • save all.

step 6 :-

Give you app some important permission like

image

note :- these permissions let bot/app to use MS graph API, in this template this is only using for gathering user email, when user install bot to perform messaging tasks.

Additional Features

  1. including handle "message extensions" commented line of code. so you can handle this type of stuff👇

image

check current conversation isBotsConversation(context) on handleContext file ---------

(⚠️⚠️⚠️ --- !important cases 🤖bot response based on "conversation ID"🤖 -- ⚠️⚠️⚠️)

when user perform something in bot's conversation/bot's "1to1" chat

  • then bots usually response user in its conversation bcs bot is part of its own conversation)
  • but if a user perform bot's tasks out of "1to1" chat, like (message extension in teams) from other conversation/chat, group, channel, etc. then bot doesn't know where to reply ('because bot may not be part of that conversation/chat').

❌❌ until you manually install bot in all chats, group, teams one by one ❌❌ (no way right) but we set our scope only personal

so, preserve the conversation ID manually.

  • we have to override that conversation ID with bots conversation id but we cannot find it in context, because we are on other chat/conversation right. so,
  1. we run isBotsConversation(context) - it tell us we are "user-to-bot" or "user-to-group", "user-to-chat", "user-to-channel", etc
  2. If we are not inside bot tab, then simply take "userEmail" in this situation.
  3. and retrive "bot's conversationID" from "database or whatever" where we store (userEmail & Bot's conmversationID) earlier based on "userEmail".
  4. then replace non bot conversationID with retrieved one.

note:- you can store userEmail & Bot's conmversationID when anyone install your bot after some customization in handleContext/handleInstallation.js file.

summary :- performing "message exention" in bot conversation "user-to-Bot", will give response in that conversation. performing "message exention" in other conversation "group/user-to-other-person", will give response in bot's conversation.

handleContext.js /* async function isBotsConversation(context) { try { const members = await context.adapter.getConversationMembers(context); if (members) { return true; } } catch (error) { return false; } } */

/* const isWeAreInBotsConversation = await isBotsConversation(context); if(isWeAreInBotsConversation === false){ resConvID = await retrieveUsersConversationID(userEmail); conversationID = resConvID.conversationID }

console.log("current Conversation ID : " + conversationID); */

this bot's template is your playground! You can customize it to any level 🚀🤖🤖

About

A teams app bot backend created with help of express, helps us to easily setup more than one bots without using azure functions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published