-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalert.py
More file actions
66 lines (50 loc) · 1.57 KB
/
Copy pathalert.py
File metadata and controls
66 lines (50 loc) · 1.57 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
import sqlite3
import requests
TG_TOKEN = '<>'
bot_name = 'https://t.me/AlertPython3000Bot'
# Create or connect to a SQLite database
db_conn = sqlite3.connect("DB.db")
cursor = db_conn.cursor()
channel_id = '-1001701550581'
# Define the query
query = """
SELECT
count(hanging_orders.order_id) as hanging_orders_count,
o.merchant_id
from (SELECT DISTINCT order_id
from Transactions
where
datetime(created_date) < strftime('%Y-%m-%d %H:%M:%S', 'now', '-7 days')
and datetime(created_date) > strftime('%Y-%m-%d %H:%M:%S', 'now', '-10 days')
and transaction_type = 'auth'
EXCEPT
SELECT DISTINCT order_id
from Transactions
where datetime(created_date) > strftime('%Y-%m-%d %H:%M:%S', 'now', '-7 days')
and transaction_type != 'auth') as hanging_orders
join Orders o on o.order_id = hanging_orders.order_id
group by o.merchant_id
"""
# Execute the query
result = cursor.execute(query)
# Fetch all rows
query_result = result.fetchall()
# Fetch column names
column_names = [description[0] for description in result.description]
# Close the database connection
db_conn.close()
# Print column names
print("\t".join(column_names))
# Print the result
alerts = [dict(zip(column_names, row)) for row in query_result]
data = '\n'.join(map(str, alerts))
alert_message = f"Alerts! hanging orders found: {data}"
def send_message(message, channel_id):
payload = {
"chat_id": channel_id,
"text": message
}
url = f"https://api.telegram.org/bot{TG_TOKEN}/sendMessage"
response = requests.post(url, json=payload)
return response
send_message(alert_message, channel_id)