Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send automated messages to Microsoft Teams using Python

I want to run a script of Python and in the end send the results in a text format to a couple of employees through MS Teams

Is there any already build library that would allow me to send a message in Microsoft Teams through Python code?

like image 449
Ricardo Vilaça Avatar asked Dec 17 '19 09:12

Ricardo Vilaça


People also ask

How do you send a message to a Microsoft team using Python?

Click on the 'Incoming Webhook'. On the next screen, click on the 'Add to a team' button. On the next screen, search for the team and channel you want to add the webhook app and then click 'Set up a connector' button at the bottom. Enter the name of the connector and select the icon image and click 'create'.

How do you send an automatic message in MS Teams?

Go to your profile picture at the top of Teams and select Set status message. Select Schedule out of office at the bottom of the options. From the screen that appears, turn on the toggle next to Turn on automatic replies. Type an out of office message in the text box.

How do you integrate Python chatbot in Microsoft Teams?

In order to integrate your bot with Microsoft Teams, you need to register it at https://dev.botframework.com/bots/new and create a Microsoft App ID and Password. After you register the bot, you need to create an application and get the app's credentials.

Can I pre schedule a Microsoft Teams chat message to be sent?

Schedule messages for delayed delivery in Microsoft Teams. Send Later is an app for Microsoft Teams that lets you schedule messages, which will then be sent on your behalf at a date and time of your choice.


2 Answers

1. Create a webhook in MS Teams

Add an incoming webhook to a Teams channel:

  1. Navigate to the channel where you want to add the webhook and select (•••) More Options from the top navigation bar.
  2. Choose Connectors from the drop-down menu and search for Incoming Webhook.
  3. Select the Configure button, provide a name, and optionally, upload an image avatar for your webhook.
  4. The dialog window will present a unique URL that will map to the channel. Make sure that you copy and save the URL — you will need to provide it to the outside service.
  5. Select the Done button. The webhook will be available in the team channel.

2. Install pymsteams

pip install pymsteams 

3. Create your python script

import pymsteams myTeamsMessage = pymsteams.connectorcard("<Microsoft Webhook URL>") myTeamsMessage.text("this is my text") myTeamsMessage.send() 

More information available here:

Add a webook to MS Teams

Python pymsteams library

like image 197
AK47 Avatar answered Sep 27 '22 23:09

AK47


Send Msteams notification without an additional package.

A simple way to send messages to teams without using any external modules. This is basically under the hood of pymsteams module. It is more useful when you are using AWS Lambda as you don't have to add layers in Lambda or supply pymsteams module as a deployment package.

import urllib3 import json from socket import timeout   class TeamsWebhookException(Exception):     """custom exception for failed webhook call"""     pass   class ConnectorCard:     def __init__(self, hookurl, http_timeout=60):         self.http = urllib3.PoolManager()         self.payload = {}         self.hookurl = hookurl         self.http_timeout = http_timeout      def text(self, mtext):         self.payload["text"] = mtext         return self      def send(self):         headers = {"Content-Type":"application/json"}         r = self.http.request(                 'POST',                 f'{self.hookurl}',                 body=json.dumps(self.payload).encode('utf-8'),                 headers=headers, timeout=self.http_timeout)         if r.status == 200:              return True         else:             raise TeamsWebhookException(r.reason)   if __name__ == "__main__":     myTeamsMessage = ConnectorCard(MSTEAMS_WEBHOOK)     myTeamsMessage.text("this is my test message to the teams channel.")     myTeamsMessage.send() 

reference: pymsteams

like image 39
nirojshrestha019 Avatar answered Sep 28 '22 00:09

nirojshrestha019