-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathngrokwebhook.py
More file actions
executable file
·120 lines (92 loc) · 4.03 KB
/
Copy pathngrokwebhook.py
File metadata and controls
executable file
·120 lines (92 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Sample script to read local ngrok info and create a corresponding webhook.
Sample script that reads ngrok info from the local ngrok client api and creates
a Webex Webhook pointint to the ngrok tunnel's public HTTP URL.
Typically ngrok is called run with the following syntax to redirect an
Internet accesible ngrok url to localhost port 8080:
$ ngrok http 8080
To use script simply launch ngrok, and then launch this script. After ngrok is
killed, run this script a second time to remove webhook from Webex.
Copyright (c) 2016-2024 Cisco and/or its affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
__author__ = "Brad Bester"
__author_email__ = "brbester@cisco.com"
__contributors__ = ["Chris Lunsford <cm@lunsford.io>"]
__copyright__ = "Copyright (c) 2016-2024 Cisco and/or its affiliates."
__license__ = "MIT"
import sys
from webexpythonsdk import WebexAPI
import requests
# Find and import urljoin
if sys.version_info[0] < 3:
from urlparse import urljoin
else:
from urllib.parse import urljoin
# Constants
NGROK_CLIENT_API_BASE_URL = "http://localhost:4040/api"
WEBHOOK_NAME = "ngrok_webhook"
WEBHOOK_URL_SUFFIX = "/events"
WEBHOOK_RESOURCE = "messages"
WEBHOOK_EVENT = "created"
def get_ngrok_public_url():
"""Get the ngrok public HTTP URL from the local client API."""
try:
response = requests.get(
url=NGROK_CLIENT_API_BASE_URL + "/tunnels",
headers={"content-type": "application/json"},
)
response.raise_for_status()
except requests.exceptions.RequestException:
print(
"Could not connect to the ngrok client API; "
"assuming not running."
)
return None
else:
for tunnel in response.json()["tunnels"]:
if tunnel.get("public_url", "").startswith("http://"):
print("Found ngrok public HTTP URL:", tunnel["public_url"])
return tunnel["public_url"]
def delete_webhooks_with_name(api, name):
"""Find a webhook by name."""
for webhook in api.webhooks.list():
if webhook.name == name:
print("Deleting Webhook:", webhook.name, webhook.targetUrl)
api.webhooks.delete(webhook.id)
def create_ngrok_webhook(api, ngrok_public_url):
"""Create a Webex webhook pointing to the public ngrok URL."""
print("Creating Webhook...")
webhook = api.webhooks.create(
name=WEBHOOK_NAME,
targetUrl=urljoin(ngrok_public_url, WEBHOOK_URL_SUFFIX),
resource=WEBHOOK_RESOURCE,
event=WEBHOOK_EVENT,
)
print(webhook)
print("Webhook successfully created.")
return webhook
def main():
"""Delete previous webhooks. If local ngrok tunnel, create a webhook."""
api = WebexAPI()
delete_webhooks_with_name(api, name=WEBHOOK_NAME)
public_url = get_ngrok_public_url()
if public_url is not None:
create_ngrok_webhook(api, public_url)
if __name__ == "__main__":
main()