forked from NexInfinite/supporter-discord-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhooks.py
69 lines (62 loc) · 2.77 KB
/
webhooks.py
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
import asyncio
import aiohttp
from quart import Quart
from hypercorn.config import Config
from hypercorn.asyncio import serve
from quart import request
app = Quart(__name__)
@app.route("/", methods=["POST"])
async def webhook():
webhook_id = request.args.get("webhook_id")
webhook_auth = request.args.get("webhook_auth")
color = request.args.get("color")
if color is None:
color = 222934
data = await request.get_json()
try:
if data["action"] == "created":
sponsorship = data["sponsorship"]
tier_description = sponsorship["tier"]["description"]
username = sponsorship["sponsor"]["login"]
privacy = sponsorship["privacy_level"]
price_in_dollars = int(sponsorship["tier"]["monthly_price_in_cents"])/100
supporter_url_html = sponsorship["sponsor"]["html_url"]
supporter_icon = sponsorship["sponsor"]["avatar_url"]
webhook_send_json = {
"embeds": [
{
"description": f"{tier_description}",
"color": color,
"author": {
"name": f"{username if privacy == 'public' else 'Anonymous'} just sponsored for ${price_in_dollars}",
"url": f"{supporter_url_html if privacy == 'public' else 'https://github.com/community-network'}",
"icon_url": f"{supporter_icon if privacy == 'public' else 'https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png'}"
},
"footer": {
"text": "Thank you for supporting us, it really helps out"
}
}
]
}
async with aiohttp.ClientSession() as session:
await session.post(f"https://discordapp.com/api/webhooks/{webhook_id}/{webhook_auth}", data=webhook_send_json)
return "Successfully sent webhook to discord!"
except Exception as e:
if data["hook"]["type"] == "SponsorsListing":
send_json = {
"content": "Github sponsor webhooks have now been added."
}
async with aiohttp.ClientSession() as session:
await session.post(f"https://discordapp.com/api/webhooks/{webhook_id}/{webhook_auth}", data=send_json)
return "Setup complete! You will now receive notifications when someone supports you on github!"
else:
return e
@app.route("/healthcheck")
def health_check():
return {"status": "ok"}
if __name__ == "__main__":
config = Config.from_mapping(
bind="0.0.0.0:80",
statsd_host="0.0.0.0:80",
)
asyncio.run(serve(app, config))