def listWebHooks(bot_token): ''' Simply list all the available Web Hooks ''' response = apiCallReturnJSON("GET", BASE_URL, getBotToken(config_file), "application/json", {}) print(json.dumps(response, indent=4, sort_keys=True))
def sendImgFromURL(room_id, img_url, message): ''' Image cannot be sent as a URL if send as a message. So I thought, why not just download the image from the URL first, and then save it as tmp.png And then attach it to a message and send it. Very messy but it works! Side note: Cards support URL images but are very strict about size of an image so it does not work most of the time. Send a message with image to a specified room. ''' # Get image URL image_response = requests.get(img_url) # Save it to a tmp file file = open("tmp.png", "wb") file.write(image_response.content) file.close() # Need to add messages to base url to indicate we are sending a message url = "{}/messages".format(BASE_URL) # When attaching image payload needs to be MultipartEncoder encodded payload = MultipartEncoder({ "roomId": room_id, "text": message, "files": ('tmp.png', open('tmp.png', 'rb'), 'image/png') }) # Make sure payload is appropriate as well content_type = payload.content_type # Do a post to send a message response = apiCallReturnJSON("POST", url, BOT_TOKEN, content_type, payload)
def createWebHook(bot_token): ''' Use information from webhook_config.json to create a new Web Hook. Display information about the new Web Hook. ''' payload = json.dumps(getHookConfig()) response = apiCallReturnJSON("POST", BASE_URL, bot_token, "application/json", payload) print(json.dumps(response, indent=4, sort_keys=True))
def getActionInfo(action_id): ''' Get information about specific atttachement ''' url = "{}/attachment/actions/{}".format(BASE_URL, action_id) payload = {} return apiCallReturnJSON("GET", url, BOT_TOKEN, "application/json", payload)
def getMsgInfo(message_id): ''' Get information about specific message. ''' url = "{}/messages/{}".format(BASE_URL, message_id) payload = {} return apiCallReturnJSON("GET", url, BOT_TOKEN, "application/json", payload)
def sendMessage(room_id, message): ''' Just send a message to specified room ''' url = "{}/messages".format(BASE_URL) payload = {"roomId": room_id, "text": message} payload = json.dumps(payload) response = apiCallReturnJSON("POST", url, BOT_TOKEN, "application/json", payload)
def updateWebHook(webhook_id, targetUrl, bot_token): ''' Updates only the targetUrl for the specific web hook. This is mostly used if you are using free version of ngrok and have different url everytime you run the code. The Web Hook will reach out to this targetUrl when triggered. ''' webhook = getWebHook(webhook_id, bot_token) # All the changes are specified in the body of the request. body = { "name": "{}".format(webhook["name"]), "targetUrl": "{}".format(targetUrl) } payload = json.dumps(body) url = "{}/{}".format(BASE_URL, webhook_id) # Update the Web Hook and display response for user reference. response = apiCallReturnJSON("PUT", url, bot_token, "application/json", payload) print(json.dumps(response, indent=4, sort_keys=True))
def deleteWebHook(webhook_id, config_file): ''' Delete specified web hook ''' url = "{}/{}".format(BASE_URL, webhook_id) apiCallReturnJSON("DELETE", url, bot_token, "application/json", {})
def getWebHook(webhook_id, bot_token): ''' Get infomration about specifi web hook. ''' url = "{}/{}".format(BASE_URL, webhook_id) return apiCallReturnJSON("GET", url, bot_token, "application/json", {})
def getRoomInfo(room_id): ''' Return everything about the room. ''' url = "{}/rooms/{}".format(BASE_URL, room_id) return apiCallReturnJSON("GET", url, BOT_TOKEN, "application/json", {})
def sendCard(room_id, img_url): ''' Send a Card to a specified room. At the moment I have disabled image attachement. But it can include image from URL. Note it does worry about size of the image so it does get annoying. ''' url = "{}/messages".format(BASE_URL) # Payload is based on Adaptive Cards, which is sent as attachement. payload = { "roomId": room_id, "text": " ", "attachments": [{ "contentType": "application/vnd.microsoft.card.adaptive", "content": { "type": "AdaptiveCard", "version": "1.0", "body": [ # { # "type": "Image", # "altText": "", # "url": "{}".format(img_url) # }, { "type": "TextBlock", "text": "In the chat just type one of the following: panda, dog, cat", "wrap": True }, { "type": "TextBlock", "text": "Otherwise just select from dropdown menu below!", "wrap": True }, { "type": "Input.ChoiceSet", "placeholder": "What would you like to see next?", "choices": [{ "title": "Panda", "value": "panda" }, { "title": "Dog", "value": "dog" }, { "title": "Cat", "value": "cat" }, { "title": "I'm Aussie", "value": "koala" }], "value": "", "id": "nextAnimal" }, { "type": "TextBlock", "text": "Would you like to see something else?" }, { "type": "Input.Text", "id": "userWish", "placeholder": "Type what would you like to see..." }, { "type": "ActionSet", "actions": [{ "type": "Action.Submit", "title": "Give me another one" }] } ], "$schema": "http://adaptivecards.io/schemas/adaptive-card.json" } }] } # Payload is still send as JSON payload = json.dumps(payload) response = apiCallReturnJSON("POST", url, BOT_TOKEN, "application/json", payload)