#!/usr/bin/env python import json, os, httplib, urllib, ssl, copy # import stock icons try: stock_icons = json.loads(open('icons.json','r').read()) except: print('Unable to load icon file') os._exit(1) # The Actual class class MessageCard: " Create and send a Teams Webhook " card_template = { '@type': 'MessageCard', '@context': 'http://schema.org/extensions', 'summary': '', 'sections': [], 'potentialAction': [] } action_button_template = { '@type': 'ActionCard', 'actions': [{ '@type': 'HttpPOST', }] } channel_uri = None icon = '' title = '' subtitle = '' facts = [] actions = [] error = '' def __init__(self, tls_verify_host=False, tls_verify_mode=ssl.CERT_NONE): try: self.SetupTLS(tls_verify_host,tls_verify_mode) except: print('Unable to setup TLS/SSL Context') os._exit(1) def SetupTLS(self,tls_verify_host,tls_verify_mode): self.tls_ctx = ssl.create_default_context() self.tls_ctx.check_hostname = tls_verify_host self.tls_ctx.verify_mode = tls_verify_mode def AddFacts(self, data_array): " Add a 'fact' to the message" if isinstance(data_array,dict): for key, value in data_array.items(): self.facts.append({'name': key, 'value': value}) if isinstance(data_array,list): for fact in data_array: if isinstance(fact,dict): for key, value in fact.items(): self.facts.append({'name': key, 'value': value}) def AddButton(self, action_array): " Add a button, expects a dict with action_name, button_label and post_uri " thisAction = None thisAction = copy.deepcopy(self.action_button_template) if isinstance(action_array,dict): try: thisAction['name'] = action_array['action_name'] except: raise Exception('action_name not present') try: thisAction['actions'][0]['name'] = action_array['button_label'] except: raise Exception('button_label not present') try: thisAction['actions'][0]['target'] = action_array['post_uri'] except: raise Exception('post_uri not present') self.actions.append(thisAction) else: self.error = 'No data provided for action' return False def GenerateCard(self): " Generate the JSON for the card " self.card = self.card_template self.card['sections'].append({}) self.card['sections'][0]['activityTitle'] = self.title self.card['sections'][0]['activitySubtitle'] = self.subtitle self.card['sections'][0]['activityImage'] = self.icon self.card['sections'][0]['facts'] = self.facts self.card['sections'][0]['potentialAction'] = self.actions self.card['sections'][0]['markdown'] = 'true' self.card['summary'] = self.title def SendCard(self): " Send the Card to the O365 API " self.GenerateCard() uri = self.channel_uri.split('/') if not len(uri) == 8: self.error = 'Invalid Channel URI' return False endpoint = uri[2] del uri[2], uri[1], uri[0] uri = '/' + str('/'.join(uri)) try: hookconnection = httplib.HTTPSConnection(endpoint, context=self.tls_ctx) except: self.error = 'Unable to open connection' return False hookconnection.request('POST',uri,json.dumps(self.card)) self.response = hookconnection.getresponse() #print(self.response.read()) def PrintJSON(self): " Print the JSON of the card " self.GenerateCard() print(json.dumps(self.card,indent=1,sort_keys=True)) # actionReq.request('PATCH','/ecx/v3/l2/connections/'+str(connid)+'?action=reject',body,headers) #teams_api = httplib.HTTPSConnection(apiEndpoint, timeout=options.GLOBAL_TIMEOUT, context=ssl_ctx)