-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
64 lines (46 loc) · 1.85 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
"""
Copyright (c) 2020 Mohamed Zumair <[email protected]>.
This file is part of Jilebi-Bot.
Jilebi-Bot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.
Jilebi-Bot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from os import environ
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
from flask import Flask, request, jsonify
from telebot.types import Update
from jilebi import jilebi, send_notification, TeleUsers
server = Flask(__name__)
scheduler = BackgroundScheduler()
scheduler.add_job(
send_notification, IntervalTrigger(minutes=30, timezone="Asia/Colombo")
)
@server.route("/" + environ.get("JILEBI_TOKEN"), methods=["POST"])
def get_message():
jilebi.process_new_updates([Update.de_json(request.stream.read().decode("utf-8"))])
return "POST", 200
@server.route("/")
def web_hook():
jilebi.remove_webhook()
jilebi.set_webhook(
url="https://jilebibot.herokuapp.com/" + environ.get("JILEBI_TOKEN")
)
return "CONNECTED", 200
@server.route("/get_user_count", methods=["GET"])
def get_user_count():
return jsonify(count=TeleUsers.objects.count())
def main():
scheduler.start()
try:
server.run(host="0.0.0.0", port=environ.get("PORT", 5000))
except KeyboardInterrupt:
print("Shutdown the bot...")
if __name__ == "__main__":
main()