-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
119 lines (88 loc) · 3.52 KB
/
app.py
File metadata and controls
119 lines (88 loc) · 3.52 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import logging
# System libraries
import os
# Scheduler
from time import sleep
import schedule
# Telegram
import telegram
from telegram.ext import Updater, Filters, MessageHandler, CommandHandler, ConversationHandler, CallbackContext
import firebase_admin
from firebase_admin import credentials
from schedules import schedule as sc
# Conf
from conf import conf_bot, conf_db
PORT = int(os.environ.get('PORT', 80))
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def get_access(id_chat, access) -> bool:
# 0 -> admin
# 1 -> admin & Group
# 2 -> all
if access == 0:
return id_chat in conf_bot.id_chat_admin
if access == 1:
return id_chat in conf_bot.id_chat_group or id_chat in conf_bot.id_chat_admin
if access == 2:
return True
return False
def report(update, context: CallbackContext, access: str):
update.message.reply_text("Maaf bos tidak boleh akses kesini, silahkan lewat group!")
tmp_message = "⚠️ <b>Lapor BOSS!!</b> ⚠️\n\n"
tmp_message += f"💀 Ada yang coba akses ke function {access}!!\n\n"
tmp_message += "😎 " + "Username : " + str(update.message.from_user.username) + "\n"
tmp_message += "🆔 " + "User ID : " + str(update.message.chat_id) + "\n"
for id_chat in conf_bot.id_chat_admin:
context.bot.send_message(chat_id=id_chat, text=tmp_message, parse_mode=telegram.ParseMode.HTML)
def schedule_checker():
while True:
schedule.run_pending()
sleep(1)
def cancel(update):
update.message.reply_text('canceled')
# end of conversation
return ConversationHandler.END
def never_sleep(context: CallbackContext):
count_job = len(context.job.job_queue.jobs())
context.bot.send_message(chat_id=-776645059,
text='Never Sleep, Schedule Count = ' + str(count_job))
if count_job == 1:
sc.remove_update_schedule(context)
def main():
# Fetch the service account key JSON file contents
cred = credentials.Certificate(conf_db.CRED_FIREBAE)
# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
'databaseURL': conf_db.DATABASE_URL
})
updater = Updater(conf_bot.TOKEN, request_kwargs={'read_timeout': 20, 'connect_timeout': 20}, use_context=True)
dip = updater.dispatcher
# Never Sleep Heroku
job = updater.job_queue
job.run_repeating(never_sleep, interval=240, first=120)
# Schedule
dip.add_handler(CommandHandler('schedule', sc.get_schedule))
dip.add_handler(CommandHandler('schedule_today', sc.get_schedule_today))
dip.add_handler(CommandHandler('update_schedule', sc.update_schedule, pass_job_queue=True))
conv_handler = ConversationHandler(
entry_points=[CommandHandler('add_schedule', sc.insert_schedule)],
states={
sc.SCHEDULE: [MessageHandler(Filters.text, sc.schedule_text)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
dip.add_handler(conv_handler)
if os.environ["DEBUG"] == 'true':
# Start the Bot Heroku
updater.start_webhook(listen="0.0.0.0",
port=PORT,
url_path=conf_bot.TOKEN,
webhook_url='https://ugmbot.herokuapp.com/' + conf_bot.TOKEN)
else:
# Start the Bot Local
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()