-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
58 lines (42 loc) · 1.46 KB
/
app.py
File metadata and controls
58 lines (42 loc) · 1.46 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
import json
import telebot
from flask import Flask, request
from config import config
from tasks import run_task
bot = telebot.TeleBot(config["TELEGRAM_TOKEN"])
app = Flask(__name__)
TOKEN = config["TELEGRAM_TOKEN"]
@bot.message_handler(commands=["start"])
def start(message):
bot.reply_to(message, "Upload chat history for parsing data")
@bot.message_handler(content_types=["document"])
def handle_docs(message):
# file_name = message.document.file_name
file_id_info = bot.get_file(message.document.file_id)
downloaded_file: bytes = bot.download_file(file_id_info.file_path)
# in memory
file = json.loads(downloaded_file.decode("utf-8"))
bot.reply_to(
message,
"Your data is analyzing. Bot will message you then the result will be ready.",
)
# Put data in Celery App
run_task.delay(reply_to=message.from_user.id, data_dict=file)
@app.route("/" + TOKEN, methods=["POST"])
def get_message():
json_string = request.get_data().decode("utf-8")
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return "!", 200
@app.route("/echo", methods=["GET"])
def echo():
return "", 204
@app.route("/")
def webhook():
bot.remove_webhook()
bot.set_webhook(url="https://tg-chat-analytics.herokuapp.com/" + TOKEN)
return (
'<center><h1><a href="https://t.me/chat_stats_analytics_bot">'
"https://t.me/chat_stats_analytics_bot</a></h1></center>",
200,
)