diff --git a/teamswebhook.py b/teamswebhook.py index d78a9bd..5880bb7 100644 --- a/teamswebhook.py +++ b/teamswebhook.py @@ -24,7 +24,37 @@ class MessageCard: 'actions': [{ '@type': 'HttpPOST', }] + } + action_textinput_template = { + '@type': 'ActionCard', + 'inputs': [{ + '@type': 'TextInput', + }], + 'actions': [{ + '@type': 'HttpPOST', + }] + } + + action_dateinput_template = { + '@type': 'ActionCard', + 'inputs': [{ + '@type': 'DateInput', + }], + 'actions': [{ + '@type': 'HttpPOST', + }] + } + + action_multichoice_template = { + '@type': 'ActionCard', + 'inputs': [{ + '@type': 'MultichoiceInput', + 'choices': [] + }], + 'actions': [{ + '@type': 'HttpPOST', + }] } channel_uri = None @@ -80,6 +110,125 @@ class MessageCard: else: raise Exception('No data provided for action') + def AddTextInput(self, action_array): + " Add a Text Input, expects a dict with action_id, action_name, button_label, input_hint and post_uri, optional boolean multiline" + thisAction = None + thisAction = copy.deepcopy(self.action_textinput_template) + if isinstance(action_array,dict): + try: + thisId = action_array['action_id'] + thisAction['inputs'][0]['id'] = thisId + thisAction['actions'][0]['body'] = str(thisId+"={{"+thisId+".value}}") + except: + raise Exception('action_id not present') + 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') + try: + thisAction['inputs'][0]['title'] = action_array['input_hint'] + except: + raise Exception('input_hint not present') + try: + if action_array['multiline'] == True: + thisAction['inputs'][0]['isMultiline'] = 'true' + except: + pass + self.actions.append(thisAction) + else: + raise Exception('No data provided for action') + + def AddDateInput(self, action_array): + " Add a Date Input, expects a dict with action_id, action_name, button_label, input_hint and post_uri, optional boolean includetime" + thisAction = None + thisAction = copy.deepcopy(self.action_dateinput_template) + if isinstance(action_array,dict): + try: + thisId = action_array['action_id'] + thisAction['inputs'][0]['id'] = thisId + thisAction['actions'][0]['body'] = str(thisId+"={{"+thisId+".value}}") + except: + raise Exception('action_id not present') + 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') + try: + thisAction['inputs'][0]['title'] = action_array['input_hint'] + except: + raise Exception('input_hint not present') + + + try: + if action_array['includetime'] == True: + thisAction['inputs'][0]['includeTime'] = 'true' + if action_array['includeTime'] == True: + thisAction['inputs'][0]['includeTime'] = 'true' + except: + pass + self.actions.append(thisAction) + else: + raise Exception('No data provided for action') + + def AddMultichoice(self, action_array): + " Add a Multichoice Input, expects a dict with action_id, action_name, button_label, input_hint, post_uri and choices list (with display/value pairs as dicts), optional boolean multiselect" + thisAction = None + thisAction = copy.deepcopy(self.action_multichoice_template) + if isinstance(action_array,dict): + try: + thisId = action_array['action_id'] + thisAction['inputs'][0]['id'] = thisId + thisAction['actions'][0]['body'] = str(thisId+"={{"+thisId+".value}}") + except: + raise Exception('action_id not present') + 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') + try: + thisAction['inputs'][0]['title'] = action_array['input_hint'] + except: + raise Exception('input_hint not present') + try: + if isinstance(action_array['choices'],list): + thisAction['inputs'][0]['choices'] = action_array['choices'] + except: + raise Exception('choices list not present') + try: + if action_array['multiselect'] == True: + thisAction['inputs'][0]['isMultiSelect'] = 'true' + if action_array['multiSelect'] == True: + thisAction['inputs'][0]['isMultiSelect'] = 'true' + except: + pass + self.actions.append(thisAction) + else: + raise Exception('No data provided for action') + def GenerateCard(self): " Generate the JSON for the card " self.card = self.card_template