|
| 1 | +#sample script that reads ngrok info from localhost:4040 and create Cisco Spark Webhook |
| 2 | +#typicall ngrok is called "ngrok http 8080" to redirect localhost:8080 to Internet |
| 3 | +#accesible ngrok url |
| 4 | +# |
| 5 | +#To use script simply launch ngrok, then launch this script. After ngrok is killed, run this |
| 6 | +#script a second time to remove webhook from Cisco Spark |
| 7 | + |
| 8 | + |
| 9 | +import requests |
| 10 | +import json |
| 11 | +import re |
| 12 | +import sys |
| 13 | +import requests.packages.urllib3 |
| 14 | +requests.packages.urllib3.disable_warnings() |
| 15 | +from ciscosparkapi import CiscoSparkAPI, Webhook |
| 16 | + |
| 17 | +def findwebhookidbyname(api, webhookname): |
| 18 | + webhooks = api.webhooks.list() |
| 19 | + for wh in webhooks: |
| 20 | + # print (room['title']) |
| 21 | + if wh.name == webhookname: |
| 22 | + return wh.id |
| 23 | + else: |
| 24 | + return "not found" |
| 25 | + |
| 26 | +#Webhook attributes |
| 27 | +webhookname="testwebhook" |
| 28 | +resource="messages" |
| 29 | +event="created" |
| 30 | +url_suffix="/sparkwebhook" |
| 31 | + |
| 32 | +#grab the at from a local at.txt file instead of global variable |
| 33 | +fat=open ("at.txt","r+") |
| 34 | +at=fat.readline().rstrip() |
| 35 | +fat.close |
| 36 | + |
| 37 | +api = CiscoSparkAPI(at) |
| 38 | + |
| 39 | +#go to the localhost page for nogrok and grab the public url for http |
| 40 | +try: |
| 41 | + ngrokpage = requests.get("http://127.0.0.1:4040").text |
| 42 | +except: |
| 43 | + print ("no ngrok running - deleting webhook if it exists") |
| 44 | + whid=findwebhookidbyname(api, webhookname) |
| 45 | + if "not found" in whid: |
| 46 | + #create |
| 47 | + print "no webhook found" |
| 48 | + sys.exit() |
| 49 | + else: |
| 50 | + #update |
| 51 | + print whid |
| 52 | + dict=api.webhooks.delete(whid) |
| 53 | + print dict |
| 54 | + sys.exit() |
| 55 | + |
| 56 | +for line in ngrokpage.split("\n"): |
| 57 | + if "window.common = " in line: |
| 58 | + ngrokjson = re.search('JSON.parse\(\"(.+)\"\)\;',line).group(1) |
| 59 | + ngrokjson = (ngrokjson.replace('\\','')) |
| 60 | +print ngrokjson |
| 61 | +targetUrl = (json.loads(ngrokjson)["Session"]["Tunnels"]["command_line (http)"]["URL"])+url_suffix |
| 62 | +print targetUrl |
| 63 | + |
| 64 | +#check if the webhook exists by name and then create it if not |
| 65 | +whid=findwebhookidbyname(api, webhookname) |
| 66 | + |
| 67 | +if "not found" in whid: |
| 68 | + #create |
| 69 | + print "not found" |
| 70 | + dict=api.webhooks.create(webhookname, targetUrl, resource, event) |
| 71 | + print dict |
| 72 | +else: |
| 73 | + #update |
| 74 | + print whid |
| 75 | + dict=api.webhooks.update(whid, webhookname, targetUrl) |
| 76 | + print dict |
0 commit comments