Send automated messages to Microsoft Teams using Python

Ricardo Vilaça picture Ricardo Vilaça · Dec 17, 2019 · Viewed 18.9k times · Source

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?

Answer

AK47 picture AK47 · Dec 17, 2019

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 pymstreams

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