How to add Bot to group chat and send message using the Bot in that group chat.

Namitha M 20 Reputation points
2023-10-02T07:01:52.06+00:00

I have created a Bot in MS Teams using Python Bot Framework, which is a one way communication ("isNotificationOnly":True). Using MS Graph API, I am able to create create group chat and fetch the ID, but bot is not added to that group chat.

I have tried multiple ways to add bot to group chat and send message using it, but nothing worked.

Please help me if anyone has tried this before - How to create group chat along with bot and send message to users using this Bot?

Microsoft Teams Development
Microsoft Security Microsoft Graph
Microsoft Teams Microsoft Teams for business Other
{count} votes

Accepted answer
  1. Nivedipa-MSFT 3,646 Reputation points Microsoft External Staff Moderator
    2023-10-03T13:08:50.92+00:00

    @Namitha M - Please follow below steps to add Bot to group chat and send message using the Bot in that group chat.

    Create a bot: You need to create a bot using the Bot Framework SDK for Python. Make sure to set the "isNotificationOnly" property to False if you want the bot to interact with users.

    Register the bot with Microsoft Teams: After creating the bot, you need to register it with Microsoft Teams. This will provide you with a Bot ID that you can use to add the bot to a group chat.

    Create a group chat: You can create a group chat using the Microsoft Graph API. The API will return a chat ID that you can use to add the bot to the chat.

    Add the bot to the group chat: To add the bot to the group chat, you can use the Microsoft Graph API. You need to make a POST request to the /chats/{chat-id}/members endpoint with the Bot ID in the request body.

    Send a message using the bot: To send a message using the bot, you can use the Bot Framework SDK for Python. You need to create a message activity and send it to the group chat using the chat ID.

    Here's a sample code snippet to send a message using the bot:

    from botbuilder.core import TurnContext
    
    async def send_message(turn_context: TurnContext):
        message = MessageFactory.text("Hello, I'm your bot!")
        await turn_context.send_activity(message)
    

    Ref Doc:

    1. https://learn.microsoft.com/en-us/microsoftteams/platform/sbs-teams-conversation-bot?tabs=dev
    2. https://learn.microsoft.com/en-us/graph/api/resources/chat?view=graph-rest-1.0

    Thanks,

    Nivedipa


    If the response is helpful, please click "Accept Answer" and upvote it. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.