Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

Azure
Home›Azure›Post Messages to Microsoft Teams Using Python

Post Messages to Microsoft Teams Using Python

By SibeeshVenu
October 1, 2022
0
0
Share:

Most of us uses Microsoft Teams on a daily basis, and we post a lot of messages, chats, contents to the platform. However there are times that you may need to post these contents automatically. Here in this post, we will see how we can post content to channel in Microsoft Teams using Python. Let’s get started.

Prerequisites

  1. You will need Python installed on your machine
  2. You will need to create a Team and a channel in the Team

Setting up a channel

Creating a channel

You will need to set up Teams before you create a channel, and if you have enough access, it is an easy thing to do. Once the Team is ready to use, you can either use one of the existing channel, or create a new one from the context menu.

Teams Context Menu

Setting up the connector

When you have decided which channel to use, click on the three dot on the right-side of your channel name, and then click on the Connectors and then search for the connector “Incoming Webhook” and then click on the button “Add”.

The above process might take a few minutes. If you are unable to perform this, you can try using the web version of Microsoft Teams.

Use of web verstion of Microsoft Teams

The above step will add the connector to your Teams, so that you can start configuring your Webhook. Click on the Configure button.

Configure Button

Provide a meaningful name for your Webhook and upload an image if required. Finally click on the Create button. A unique Url will be generated for your Webhook. Copy the Url and save it somewhere, we will later add this to Azure Key Vault and use from there. Click on the Done button. Your Webhook now should be shown as configured.

Webhook Configured

Post Message Using the Connector

Setting up Azure Key Vault

Configuring the Azure Key Vault is as easy as you set up any other Azure Resource. You may get the steps required from this post or this. Add a new secret and paste the Webhook Url value you copied earlier.

Make sure that you had set up the Access Policies on your Azure Key vault.

Access Policy

Copy your Azure Key Vault name and the Secret name, we will use this in our Python function.

Setting up Python Application

Let’s set up our Python application. Create a directory msteams-webhook and create a file requirements.txt with the preceeding contents.

pymsteams[async]
azure-identity
azure-keyvault-secrets

Now run the commands below to create a virtual environment, activate the environment and then install the requirements in our environment. If you get a message like “WARNING: You are using pip version 19.2.3, however version 22.2.2 is available.”, you can upgrade pip by running the command python -m pip install --upgrade pip

py -m venv .venv
.\.venv\Scripts\activate
py -m pip install -r requirements.txt

You can learn more abou the Virtual Environment here.

Python Virtual Envrronment

Get the Secret from Azure Key Vault

Let’s create a new Python file keyvault_helper.py with the codes below.

Send Message to Teams Channel

To send a message to the Teams channel, we are going to use a package pymsteams. This has been already added to our requirements file. Let’s create a file notify_teams.py with the codes below.

As we have all the set up done, let’s create main.py file with the codes below to start sending the messages to the Teams channel.

Before you run the main file, make sure that you have logged in to the terminal using az login with the account that you had set the access policy in your azure key vault. We can also set this access policies by using a managed identity, or a service principal, so that we can use it for more production scenarios. But for this post, let’s stick with a user account.

If you get the error, VisualStudioCodeCredential: The current credential is not configured to acquire tokens for tenant or SharedTokenCacheCredential: The current credential is not configured to acquire tokens for tenant you can set the values  exclude_shared_token_cache_credential=True or exclude_visual_studio_code_credential=True when you get tokens using DefaultAzureCredential in your get_key_vault_secret function in keyvault_helper.py file.

Keyvault Error
default.py file from Azure Identity

By this time, we can hope that you will be able to get the secret from the key vault and now we can execute py .\main.py file. Once the file is executed, you should get a response in your cli as preceding.

{"version":"1.1","content":{"headers":[{"key":"Content-Type","value":["text/plain; charset=utf-8"]}]},"statusCode":200,"reasonPhrase":"OK","headers":[],"trailingHeaders":[],"requestMessage":null,"isSuccessStatusCode":true}

You should also get a message on your Teams channel.

Message in Teams Channel

Source Code

You can also see the codes in this repository.

About the Author

I am yet another developer who is passionate about writing and video creation. I have written more than 500 blogs on my blog. If you like this content, consider following me here,

  • GitHub
  • medium
  • Twitter

Your turn. What do you think?

Thanks a lot for reading. Did I miss anything that you may think is needed in this article? Could you find this post useful? Kindly do not forget to share your feedback.

Kindest Regards

Sibeesh Venu

TagsAzureAzure Key VaultMicrosoft teamsMicrosoft Teams Webhook with PythonSend message to Microsoft TeamsTeams Python ClientTeams Webhook
Previous Article

Get Azure Blob Storage Blob Metadata Using ...

Next Article

Async Client IP safelist for Dot NET

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0

SibeeshVenu

I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

Related articles More from author

  • Creating a BLOB Container
    AzureAzure CDNHow to

    Upload Contents To Azure Container, CDN in WordPress Folder Format

    May 29, 2017
    By SibeeshVenu
  • Azure

    Show Recent Blog Posts in GitHub ReadMe using Azure Function

    July 20, 2020
    By SibeeshVenu
  • AzureIoT

    Azure Stream Analytics Job and Tools for Visual Studio

    December 13, 2018
    By SibeeshVenu
  • Azure

    Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

    April 27, 2025
    By SibeeshVenu
  • Porsche Car Result
    AzureCognitive Services

    Custom Vision AI – Building Your Own Custom Model and Train

    November 25, 2018
    By SibeeshVenu
  • Text Translator Api Thumbnail
    AzureTranslator TextVideos

    Video: Azure Cognitive Services Text Translator API

    June 30, 2018
    By SibeeshVenu
0

My book

Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

YouTube

MICROSOFT MVP (2016-2022)

profile for Sibeesh Venu - Microsoft MVP

Recent Posts

  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
  • Easily move data from one COSMOS DB to another
  • .NET 8 New and Efficient Way to Check IP is in Given IP Range
  • Async Client IP safelist for Dot NET
  • Post Messages to Microsoft Teams Using Python
  • Get Azure Blob Storage Blob Metadata Using PowerShell
  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
  • Integrate Azure App Insights in 1 Minute to .Net6 Application
  • Azure DevOps Service Connection with Multiple Azure Resource Group

Tags

Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
  • .NET
  • Achievements
  • ADO.NET
  • Android
  • Angular
  • Arduino
  • Article Of The Day
  • ASP.NET
  • Asp.Net Core
  • Automobile
  • Awards
  • Azure
  • Azure CDN
  • azure devops
  • Blockchain
  • Blog
  • Browser
  • C-Sharp Corner
  • C#
  • Career Advice
  • Code Snippets
  • CodeProject
  • Cognitive Services
  • Cosmos DB
  • CSS
  • CSS3
  • Data Factory
  • Database
  • Docker
  • Drawings
  • Drill Down Chart
  • English
  • Excel Programming
  • Exporting
  • Facebook
  • Fun
  • Gadgets
  • GitHub
  • GoPro
  • High Map
  • HighChart
  • How to
  • HTML
  • HTML5
  • Ignite UI
  • IIS
  • Interview
  • IoT
  • JavaScript
  • JQuery
  • jQuery UI
  • JQWidgets
  • JQX Grid
  • Json
  • Knockout JS
  • Linux
  • Machine Learning
  • Malayalam
  • Malayalam Poems
  • MDX Query
  • Microsoft
  • Microsoft ADOMD
  • Microsoft MVP
  • Microsoft Office
  • Microsoft Technologies
  • Microsoft Windows
  • Microsoft Windows Server
  • Mobile
  • MongoDB
  • Monthly Winners
  • MVC
  • MVC Grid
  • MySQL
  • News
  • Node JS
  • npm
  • Number Conversions
  • October 2015
  • Office 365
  • Office Development
  • One Plus
  • Outlook
  • Page
  • PHP
  • Poems
  • PowerShell
  • Products
  • Q&A
  • Raspberry PI
  • React
  • SEO
  • SharePoint
  • Skype
  • Social Media
  • Software
  • Spire.Doc
  • Spire.PDF
  • Spire.XLS
  • SQL
  • SQL Server
  • SSAS
  • SSMS
  • Storage In HTML5
  • Stories
  • Third Party Software Apps
  • Tips
  • Tools
  • Translator Text
  • Uncategorized
  • Unit Testing
  • UWP
  • VB.Net
  • Videos
  • Virtual Machine
  • Visual Studio
  • Visual Studio 2017
  • Wamp Server
  • Web API
  • Web Platform Installer
  • Webinars
  • WebMatrix
  • Windows 10
  • Windows 7
  • Windows 8.1
  • Wordpress
  • Writing

ABOUT ME

I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

Contact Me

  • info@sibeeshpassion.com

Pages

  • About
  • Search
  • Privacy Policy
  • About
  • Search
  • Privacy Policy
© Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
Go to mobile version