forked from WhiteHatArpit/Facebook-Slack-Integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.py
65 lines (54 loc) · 1.64 KB
/
webhook.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
import requests
import json
from parse import parse,parsenotif
from fetch import fetch,fetchnotif
# fetch, parse and post to slack
def posttoSlack(slack_url,app_token,access_token,packetm,packetn,count):
# for debugging token, check if valid
debug_url ="https://graph.facebook.com/debug_token?input_token="+ access_token +"&access_token=" + app_token
prev_packetm = packetm
prev_packetn = packetn
try:
response = requests.get(debug_url)
data = json.loads(response.text)
# validation of token
if 'error' in data.keys():
packetm = "access_token not valid"
else:
# fetch messages data
try:
data = fetch(access_token)
except:
packetm = "Couldnt Fetch"
# parse fetched message data
try:
packetm = parse(data)
except:
packetm = "Couldnt Parse"
# change this if you need to get updated for notifications more frequently
if count % 6 == 0:
# fetch notifications data
try:
data = fetchnotif(access_token)
except:
packetn = "Couldnt Fetch Notif"
# parse notifications data
try:
packetn = parsenotif(data)
except:
packetn = "Couldnt Parse Notif"
except:
packetm = "Couldnt Debug"
# prepare packet with messages and notifications
if count % 6 == 0:
# post to slack if not same as earliar
if packetn+packetm != prev_packetn+prev_packetm:
payload={"text": packetn+packetm }
response = requests.post(slack_url,json.dumps(payload))
# prepare packet with messages only
else:
# post to slack if not same as earliar
if packetm != prev_packetm:
payload={"text": packetm }
response = requests.post(slack_url,json.dumps(payload))
return packetm,packetn