The following jython code formats an Adaptive Card and posts the card to a MS Teams Channel through an Incoming Webhook Connector. It works about half the time and the other half it will return an HTTP error 401. I have tried this multiple times using the same endpoint and same variables. The print statement output for one that works and one that does not is below.
Anyone have any ideas?
Code:
import json
import httplib
def callNotificationWebhook(contactList, notifySubject, deptName, stationNickname, resourceString, message, timestamp):
formattedMessage = message
if message is None:
formattedMessage = "No details provided"
elif len(message) == 0:
formattedMessage = "No details provided"
webhookSkeleton = {
"type":"message",
"attachments":[]
}
adaptiveCardSkeleton = {
"contentType":"application/vnd.microsoft.card.adaptive",
"contentUrl":None,
"content":{
"type":"AdaptiveCard",
"$schema":"http://adaptivecards.io/schemas/adaptive-card.json",
"version":"1.2",
"body": []
}
}
heading = {
"type":"TextBlock",
"size":"Large",
"weight":"Bolder",
"text":notifySubject,
"wrap":True,
"style":"heading"
}
title = {
"type":"TextBlock",
"weight":"Bolder",
"text":"Shop Talk Notification",
"wrap":True
}
subtitle = {
"type":"TextBlock",
"spacing":"None",
"text":"Sent " + timestamp,
"isSubtle":True,
"wrap":True
}
keyFeatures = {
"type":"FactSet",
"facts":[
{
"title":"Department",
"value":deptName
},
{
"title":"Station",
"value":stationNickname
},
{
"title":"Requested",
"value":resourceString
},
{
"title":"Details",
"value":formattedMessage
}
]
}
adaptiveCardSkeleton["content"]["body"] = [heading, title, subtitle, keyFeatures]
webhookSkeleton["attachments"] = [adaptiveCardSkeleton]
webhookSkeleton["summary"] = notifySubject
for contact in contactList:
conn = httplib.HTTPSConnection("deere.webhook.office.com", 443)
conn.request("POST", contact.split(".com")[1], body=json.dumps(webhookSkeleton))
response = conn.getresponse()
print(response.status)
print(response.reason)
print(response.read())
print(response.getheaders())
conn.close()
This is the result of the print statements when successful:
200
OK
1
[('x-feproxyinfo', 'DS7PR07CA0014.NAMPRD07.PROD.OUTLOOK.COM'), ('date', 'Wed, 09 Mar 2022 18:36:56 GMT'), ('content-length', '1'), ('server', 'Microsoft-IIS/10.0'), ('expires', '-1'), ('x-beserver', 'DM6PR05MB6777'), ('x-aspnet-version', '4.0.30319'), ('x-cafeserver', 'DS7PR07CA0014.NAMPRD07.PROD.OUTLOOK.COM'), ('x-proxy-backendserverstatus', '200'), ('x-calculatedbetarget', 'DM6PR05MB6777.NAMPRD05.PROD.OUTLOOK.COM'), ('ms-cv', 'ZWXNtAOOHs/fpZcGGYmzhQ.1.1'), ('x-calculatedfetarget', 'DS7PR07CU001.internal.outlook.com'), ('x-bepartition', 'CLNAMPRD05DSM02'), ('x-powered-by', 'ASP.NET'), ('x-backendhttpstatus', '200, 200'), ('content-type', 'text/plain; charset=utf-8'), ('cache-control', 'no-cache'), ('request-id', 'b4cd6565-8e03-cf1e-dfa5-97061989b385'), ('strict-transport-security', 'max-age=31536000; includeSubDomains; preload'), ('pragma', 'no-cache'), ('x-rum-validated', '1'), ('x-feserver', 'DS7PR07CA0014, CH2PR17CA0001'), ('x-proxy-routingcorrectness', '1'), ('alt-svc', 'h3=":443",h3-29=":443"'), ('x-firsthopcafeefz', 'MDW')]
This is the result of the print statements when not successful:
200
OK
Webhook message delivery failed with error: Microsoft Teams endpoint returned HTTP error 401 with ContextId MS-CV=vYiWBe+bF0yczTkuW5GYaQ.0..
[('x-feproxyinfo', 'DM6PR03CA0088.NAMPRD03.PROD.OUTLOOK.COM'), ('date', 'Wed, 09 Mar 2022 18:41:03 GMT'), ('content-length', '140'), ('server', 'Microsoft-IIS/10.0'), ('expires', '-1'), ('x-beserver', 'DM6PR05MB6779'), ('x-aspnet-version', '4.0.30319'), ('x-cafeserver', 'DM6PR03CA0088.NAMPRD03.PROD.OUTLOOK.COM'), ('x-proxy-backendserverstatus', '200'), ('x-calculatedbetarget', 'DM6PR05MB6779.NAMPRD05.PROD.OUTLOOK.COM'), ('ms-cv', 'K+Ni6BrAVHDEAHZadzjWNg.1.1'), ('x-calculatedfetarget', 'DM6PR03CU003.internal.outlook.com'), ('x-bepartition', 'CLNAMPRD05DSM02'), ('x-powered-by', 'ASP.NET'), ('x-backendhttpstatus', '200, 200'), ('content-type', 'text/plain; charset=utf-8'), ('cache-control', 'no-cache'), ('request-id', 'e862e32b-c01a-7054-c400-765a7738d636'), ('strict-transport-security', 'max-age=31536000; includeSubDomains; preload'), ('pragma', 'no-cache'), ('x-rum-validated', '1'), ('x-feserver', 'DM6PR03CA0088, CH2PR17CA0009'), ('x-proxy-routingcorrectness', '1'), ('alt-svc', 'h3=":443",h3-29=":443"'), ('x-firsthopcafeefz', 'MDW')]