-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·74 lines (64 loc) · 2.94 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from fileinput import filename
import logging
from multiprocessing import context
import config
from xml.sax.handler import feature_namespaces
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, ConversationHandler, filters, PicklePersistence, ContextTypes
from handler_unlock import unlock
from handler_start import start
import handlers_photo
import handler_media
from flask import Flask, request
import asyncio
import http
import config
async def main() -> None:
####### Setting up logging #######
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG
)
# Creating a file to store data between requests
my_persistence = PicklePersistence(filepath = 'file_persistence')
# If webhook is True then we assume it is in container and it requires custom web server, thus we need custom UDATER, if not then run with built in UDPATER
application = ApplicationBuilder().token(config.TOKEN).updater(None).persistence(persistence=my_persistence).build()
await application.update_queue.put(
Update.de_json(data = request.get_json(force=True), bot=application.bot)
)
# adding handlers
application.add_handler(handlers_photo.photo_conv_handler)
application.add_handler(handler_media.media_conv_handler)
application.add_handler(CommandHandler('start', start))
application.add_handler(MessageHandler(filters.Text(), unlock))
# Start and run the application
async with application:
await application.start()
# when some shutdown mechanism is triggered:
await application.stop()
##########################################
# custom web server when webhook is enabled
webserver = Flask(__name__)
@webserver.route("/", methods = ['GET', 'POST'])
def index():
asyncio.run(main())
return "", http.HTTPStatus.NO_CONTENT
##########################################
# We ignore FLASK if Webhook is set up and starting in debug mode for local testing using getUdpates
# Checking Webhook parameter and starting application in getUpdates mode.
if config.WEBHOOK == False:
####### Setting up logging #######
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG
)
# Creating a file to store data between requests
my_persistence = PicklePersistence(filepath = 'file_persistence')
application = ApplicationBuilder().token(config.TOKEN).persistence(persistence=my_persistence).build()
# adding handlers
application.add_handler(handlers_photo.photo_conv_handler)
application.add_handler(handler_media.media_conv_handler)
application.add_handler(CommandHandler('start', start))
application.add_handler(MessageHandler(filters.Text(), unlock))
# Start and run the application using getUpdates mode. For local usage.
application.run_polling()