-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
55 lines (44 loc) · 1.69 KB
/
main.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
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
import logging
import aiohttp
import os
from dotenv import load_dotenv
from flask import Flask
import threading
load_dotenv()
# TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
TOKEN = ''
WEBHOOK_URL = 'https://hooks.zapier.com/hooks/catch/14479245/35hqzv0/'
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
async def send_to_webhook(data):
async with aiohttp.ClientSession() as session:
async with session.post(WEBHOOK_URL, json=data) as response:
return await response.text()
def format_contact_data(data):
return {
"user_id": data.chat.id,
"contact_user_id": data.contact.user_id,
"contact_first_name": data.contact.first_name,
"contact_last_name": data.contact.last_name,
"contact_phone_number": data.contact.phone_number
}
async def get_shared_contact(update: Update, context: ContextTypes.DEFAULT_TYPE):
contact = format_contact_data(data=update.message)
response = await send_to_webhook(contact)
print(f"Webhook response: {response}")
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
if __name__ == '__main__':
t = threading.Thread(target=lambda: app.run(host="0.0.0.0", port=int(os.getenv("PORT") or 3000)))
t.daemon = True
t.start()
application = ApplicationBuilder().token(token=TOKEN).build()
get_shared_contact_handler = MessageHandler(filters.CONTACT, get_shared_contact)
application.add_handler(get_shared_contact_handler)
application.run_polling()