<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Teams Webhook &#8211; Sibeesh Passion</title>
	<atom:link href="https://sibeeshpassion.com/tag/teams-webhook/feed/" rel="self" type="application/rss+xml" />
	<link>https://sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Sat, 01 Oct 2022 14:34:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Teams Webhook &#8211; Sibeesh Passion</title>
	<link>https://sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Post Messages to Microsoft Teams Using Python</title>
		<link>https://sibeeshpassion.com/post-messages-to-microsoft-teams-using-python/</link>
					<comments>https://sibeeshpassion.com/post-messages-to-microsoft-teams-using-python/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sat, 01 Oct 2022 14:34:39 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure Key Vault]]></category>
		<category><![CDATA[Microsoft teams]]></category>
		<category><![CDATA[Microsoft Teams Webhook with Python]]></category>
		<category><![CDATA[Send message to Microsoft Teams]]></category>
		<category><![CDATA[Teams Python Client]]></category>
		<category><![CDATA[Teams Webhook]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=14592</guid>

					<description><![CDATA[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. ]]></description>
										<content:encoded><![CDATA[
<p>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&#8217;s get started. </p>



<h1 class="wp-block-heading">Prerequisites</h1>



<ol class="wp-block-list"><li>You will need Python installed on your machine</li><li>You will need to create a Team and a channel in the Team</li></ol>



<h1 class="wp-block-heading">Setting up a channel</h1>



<h2 class="wp-block-heading">Creating a channel</h2>



<p>You will need to <a rel="noreferrer noopener" href="https://support.microsoft.com/en-us/office/create-a-team-from-scratch-174adf5f-846b-4780-b765-de1a0a737e2b" target="_blank">set up Teams before</a> 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. </p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="/wp-content/uploads/2022/10/image.png"><img fetchpriority="high" decoding="async" width="303" height="509" src="/wp-content/uploads/2022/10/image.png" alt="" class="wp-image-14594" srcset="/wp-content/uploads/2022/10/image.png 303w, /wp-content/uploads/2022/10/image-179x300.png 179w" sizes="(max-width: 303px) 100vw, 303px" /></a><figcaption>Teams Context Menu</figcaption></figure>
</div>


<h2 class="wp-block-heading">Setting up the connector</h2>



<p>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 &#8220;Incoming Webhook&#8221; and then click on the button &#8220;Add&#8221;. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>The above process might take a few minutes. If you are unable to perform this, you can try using <a href="https://teams.microsoft.com/" target="_blank" rel="noreferrer noopener">the web version of Microsoft Teams</a>.</p><cite>Use of web verstion of Microsoft Teams</cite></blockquote>



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


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2022/10/image-1.png"><img decoding="async" width="1024" height="196" src="/wp-content/uploads/2022/10/image-1-1024x196.png" alt="" class="wp-image-14595" srcset="/wp-content/uploads/2022/10/image-1-1024x196.png 1024w, /wp-content/uploads/2022/10/image-1-300x57.png 300w, /wp-content/uploads/2022/10/image-1-768x147.png 768w, /wp-content/uploads/2022/10/image-1.png 1129w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>Configure Button</figcaption></figure>
</div>


<p>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 <a rel="noreferrer noopener" href="https://sibeeshpassion.com/tag/azure-key-vault/" target="_blank">Azure Key Vault</a> and use from there. Click on the Done button. Your Webhook now should be shown as configured. </p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2022/10/image-2.png"><img decoding="async" width="1024" height="214" src="/wp-content/uploads/2022/10/image-2-1024x214.png" alt="" class="wp-image-14596" srcset="/wp-content/uploads/2022/10/image-2-1024x214.png 1024w, /wp-content/uploads/2022/10/image-2-300x63.png 300w, /wp-content/uploads/2022/10/image-2-768x161.png 768w, /wp-content/uploads/2022/10/image-2.png 1147w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>Webhook Configured</figcaption></figure>
</div>


<h1 class="wp-block-heading">Post Message Using the Connector</h1>



<h2 class="wp-block-heading">Setting up Azure Key Vault</h2>



<p>Configuring the Azure Key Vault is as easy as you set up any other Azure Resource. You may get the steps required from <a rel="noreferrer noopener" href="https://sibeeshpassion.com/why-not-secure-your-keys-and-secrets-asp-net-core-with-azure-key-vault-integration/" target="_blank">this post</a> or <a rel="noreferrer noopener" href="https://learn.microsoft.com/en-us/azure/key-vault/general/overview" target="_blank">this</a>. Add a new secret and paste the Webhook Url value you copied earlier. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Make sure that you had set up the Access Policies on your Azure Key vault.</p><cite>Access Policy</cite></blockquote>



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



<h2 class="wp-block-heading">Setting up Python Application</h2>



<p>Let&#8217;s set up our Python application. Create a directory msteams-webhook and create a file <strong>requirements.txt</strong> with the preceeding contents. </p>



<pre class="wp-block-code"><code>pymsteams&#91;async]
azure-identity
azure-keyvault-secrets</code></pre>



<p>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 &#8220;WARNING: You are using pip version 19.2.3, however version 22.2.2 is available.&#8221;, you can upgrade pip by running the command <code>python -m pip install --upgrade pip</code></p>



<pre class="wp-block-code"><code>py -m venv .venv
.\.venv\Scripts\activate
py -m pip install -r requirements.txt</code></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>You can learn more abou the Virtual Environment <a rel="noreferrer noopener" href="https://docs.python.org/3/library/venv.html" target="_blank">here</a>.</p><cite>Python Virtual Envrronment</cite></blockquote>



<h2 class="wp-block-heading">Get the Secret from Azure Key Vault</h2>



<p>Let&#8217;s create a new Python file <strong>keyvault_helper.py</strong> with the codes below. </p>



<script src="https://gist.github.com/SibeeshVenu/ce578e01a22000e430d92bd645ae95ea.js"></script>



<h2 class="wp-block-heading">Send Message to Teams Channel</h2>



<p>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&#8217;s create a file <strong>notify_teams.py</strong> with the codes below. </p>



<script src="https://gist.github.com/SibeeshVenu/f7262bb8a22c7e84041e16b3f71df7f7.js"></script>



<p>As we have all the set up done, let&#8217;s create main.py file with the codes below to start sending the messages to the Teams channel. </p>



<script src="https://gist.github.com/SibeeshVenu/62f000341b6284d2b92e1c0266647908.js"></script>



<p>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&#8217;s stick with a user account. </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>If you get the error,  <code>VisualStudioCodeCredential: The current credential is not configured to acquire tokens for tenant</code> or <code>SharedTokenCacheCredential: The current credential is not configured to acquire tokens for tenant</code> you can set the values  <code>exclude_shared_token_cache_credential=True</code> or <code>exclude_visual_studio_code_credential=True</code> when you get tokens using <code>DefaultAzureCredential</code> in your <code>get_key_vault_secret</code> function in <code>keyvault_helper.py</code> file. </p><cite>Keyvault Error</cite></blockquote>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2022/10/image-3.png"><img decoding="async" width="1024" height="201" src="/wp-content/uploads/2022/10/image-3-1024x201.png" alt="" class="wp-image-14598" srcset="/wp-content/uploads/2022/10/image-3-1024x201.png 1024w, /wp-content/uploads/2022/10/image-3-300x59.png 300w, /wp-content/uploads/2022/10/image-3-768x150.png 768w, /wp-content/uploads/2022/10/image-3.png 1302w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>default.py file from Azure Identity</figcaption></figure>
</div>


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



<pre class="wp-block-code"><code>{"version":"1.1","content":{"headers":&#91;{"key":"Content-Type","value":&#91;"text/plain; charset=utf-8"]}]},"statusCode":200,"reasonPhrase":"OK","headers":&#91;],"trailingHeaders":&#91;],"requestMessage":null,"isSuccessStatusCode":true}</code></pre>



<p>You should also get a message on your Teams channel.</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><a href="/wp-content/uploads/2022/10/image-4.png"><img decoding="async" width="1024" height="175" src="/wp-content/uploads/2022/10/image-4-1024x175.png" alt="" class="wp-image-14599" srcset="/wp-content/uploads/2022/10/image-4-1024x175.png 1024w, /wp-content/uploads/2022/10/image-4-300x51.png 300w, /wp-content/uploads/2022/10/image-4-768x131.png 768w, /wp-content/uploads/2022/10/image-4-1536x263.png 1536w, /wp-content/uploads/2022/10/image-4-2048x350.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption>Message in Teams Channel</figcaption></figure>
</div>


<h1 class="wp-block-heading">Source Code</h1>



<p>You can also see the codes in <a href="https://github.com/SibeeshVenu/msteams-webhook-py" target="_blank" rel="noreferrer noopener">this repository</a>. </p>



<h1 class="wp-block-heading">About the Author</h1>



<p>I am yet another developer who is passionate about writing and video creation. I have written more than 500 blogs on my <a rel="noreferrer noopener" href="https://sibeeshpassion.com/" target="_blank">blog</a>. If you like this content, consider following me here,</p>



<ul class="wp-block-list"><li><a href="https://github.com/SibeeshVenu">GitHub</a></li><li><a href="https://medium.com/@sibeeshvenu">medium</a></li><li><a href="https://twitter.com/sibeeshvenu">Twitter</a></li></ul>



<h1 class="wp-block-heading">Your turn. What do you think?</h1>



<p>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.</p>



<p>Kindest Regards</p>



<p>Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sibeeshpassion.com/post-messages-to-microsoft-teams-using-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
