Jamie Thomson

Thoughts, about stuff

Archive for March 2017

Post messages to a Microsoft Teams incoming webhook from behind a proxy using Python

leave a comment »

The subject of this post is a bit of a mouthful but its going to do exactly what it says on the tin.

I’ve been playing with Microsoft Teams a lot over the past few days and I wanted to programatically post messages to a channel on Microsoft Teams using the language I’m using most often these days, Python. In my case it was made complicated by the presence of my company’s proxy server, I figured out a way around it and figured I’d post the code here for anyone that might be interested.

First you’ll need to create an incoming webhook. I’ll assume that you know what that means otherwise why would you be reading this? I’ll also assume you’ve already gotten your webhook URL as per the instructions at https://msdn.microsoft.com/en-us/microsoft-teams/connectors#create-the-webhook.

After that you just need to edit the code below with the address of your proxy server (I can’t help you with that), a username to authenticate to your proxy server, and your webhook URL. You will also need to have installed the requests library.

Here’s the code:


import requests #learn about requests: http://docs.python-requests.org/en/master/
from requests.auth import HTTPProxyAuth
from getpass import getpass

proxyDict = { 'http' : 'your.proxy.server:port', 'https' : 'your.proxy.server:port' }
webhook_url = "https://outlook.office.com/webhook/blahblahblah"
user = 'username'
password = getpass()
auth = HTTPProxyAuth(user, password)
json_payload = {'text' : 'hello world'}
requests.post(url=webhook_url, proxies=proxyDict, auth=auth, json=json_payload)

If it works you should see something like this posted to the channel for which you set up the incoming webhook:

2017-03-12_23-42-29

Good luck!

@jamiet

Written by Jamiet

March 12, 2017 at 11:45 pm

Posted in Uncategorized

Tagged with ,