-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathwebhook.py
124 lines (100 loc) · 3.46 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
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
121
122
123
124
import asyncio
import configparser
import flask
import json
import sqlite3
import requests
from flask import abort, request
from rastreio.db import User
config = configparser.ConfigParser()
config.sections()
BOT_CONFIG_FILE = 'bot.conf'
config.read(BOT_CONFIG_FILE)
db = config['SQLITE3']['data_base']
table = config['SQLITE3']['table']
webhook_host = config['WEBHOOK']['HOST']
webhook_port = config['WEBHOOK']['PORT']
webhook_key = config['WEBHOOK']['KEY']
meli_client_id = config['MERCADOLIVRE']['client_id']
meli_client_secret_key = config['MERCADOLIVRE']['secret_key']
meli_redirect_url = config['MERCADOLIVRE']['redirect_url']
meli_redirect_url_salt = config['MERCADOLIVRE']['redirect_url_salt']
app = flask.Flask(__name__)
def adduser(chatid='', picpayid='', sub_id=''):
conn = sqlite3.connect(db)
cursor = conn.cursor()
aux = ('''INSERT INTO {} (chatid, picpayid, sub_id)
VALUES ('{}', '{}', '{}')''').format(table, chatid, picpayid, sub_id)
cursor.execute(aux)
conn.commit()
conn.close()
def deluser(arg1, arg1_value):
conn = sqlite3.connect(db)
cursor = conn.cursor()
aux = ('''DELETE FROM {}
WHERE {} = {}''').format(table, arg1, arg1_value)
cursor.execute(aux)
conn.commit()
conn.close()
def updateuser(arg1, arg1_value, arg2, arg2_value):
conn = sqlite3.connect(db)
cursor = conn.cursor()
aux = ('''UPDATE {} SET {} = "{}"
WHERE {} = "{}"''').format(table, arg1, arg1_value, arg2, arg2_value)
cursor.execute(aux)
conn.commit()
conn.close()
def select_user(col, arg):
conn = sqlite3.connect(db)
cursor = conn.cursor()
aux = ('''SELECT * FROM {} WHERE
{} ="{}"''').format(table, col, arg)
cursor.execute(aux)
data = cursor.fetchone()
conn.close()
return data
#@app.route('/' + webhook_key + '/')
#def hello():
# return ""
@app.route('/' + webhook_key + '/', methods=['POST'])
def secbox():
jsonData = request.get_json()
if webhook_key not in str(request.headers['X-Verification-Key']):
return "Error"
if jsonData['event_type'] == 'new_subscription':
if not select_user('picpayid', jsonData['event']['subscriber']['username']):
adduser('', jsonData['event']['subscriber']['username'].lower(), jsonData['event']['subscriber']['id'])
if jsonData['event_type'] == 'subscription_cancelled':
deluser('sub_id', jsonData['event']['subscriber_id'])
return "Hello"
@app.route(f"/meli/signup/{meli_redirect_url_salt}")
def meli_signup():
code = request.args.get("code")
# Gambiarra para relacionar usuário do telegram a sua conta no meli
telegram_id = request.args.get("state")
if not telegram_id or not code:
abort(400)
url = "https://api.mercadolibre.com/oauth/token"
data = {
"grant_type": "authorization_code",
"client_id": meli_client_id,
"client_secret": meli_client_secret_key,
"code": code,
"redirect_uri": meli_client_redirect_url,
}
response = requests.post(url, json=data).json()
meli_access_token = response["access_token"]
meli_refresh_token = response["refresh_token"]
user = User.update(
telegram_id,
upsert=True,
meli_access_token=meli_access_token,
meli_refresh_token=meli_refresh_token,
)
# TODO: notify user
return ""
@app.route("/meli/notifications")
def meli_notifications():
pass
if __name__ == '__main__':
app.run(host=webhook_host, port=webhook_port)