Multithreading message handling.

This commit is contained in:
USA Hexor
2018-05-17 16:33:53 +00:00
parent 6b948d4b2b
commit 7341eaaa4e

View File

@@ -7,10 +7,15 @@ This program is dedicated to the public domain under the CC0 license.
""" """
import os import os
import logging import logging
import threading
import telegram import telegram
from telegram.error import NetworkError, Unauthorized from telegram.error import NetworkError, Unauthorized
from time import sleep from time import sleep
import tg_settings try:
import tg_settings
except:
print("You have to create tg_settings.py. See tg_settings.py.example.")
os.exit(1)
try: try:
if os.environ["DRYRUN"]: if os.environ["DRYRUN"]:
from dryrun import Perdoliq from dryrun import Perdoliq
@@ -38,7 +43,7 @@ def main():
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
while True: while True:
try: try:
echo(bot) handle_update(bot)
except NetworkError: except NetworkError:
sleep(1) sleep(1)
except Unauthorized: except Unauthorized:
@@ -64,7 +69,7 @@ def list_test(username, password):
return "Exception: " + str(e) return "Exception: " + str(e)
def echo(bot): def handle_update(bot):
"""Echo the message the user sent.""" """Echo the message the user sent."""
global update_id global update_id
# Request updates after the last update_id # Request updates after the last update_id
@@ -72,68 +77,69 @@ def echo(bot):
update_id = update.update_id + 1 update_id = update.update_id + 1
if update.message: if update.message:
try: # if there is and update, process it in threads
s = update.message.text.split() t = threading.Thread(target=do_action, args=(update,))
except: t.start()
s = "empty"
if s[0] == '/resolve':
#msg = "usr: " + \
# s[1] + " pass: " + \
# s[2] + " subj: " + \
# s[3] + " test: " + \
# s[4] + " accuracy: " + \
# s[5] + " commit: " + s[6]
if len(s) != 7:
update.message.reply_markdown("Missing operand... Try again")
update.message.reply_markdown("Usage: */resolve <user[text]> "\
"<pass[text]> <subj[int]> <test[int]> "\
"<accuracy[0-100]> <commit[1/0]>*")
return False
msg = "Please wait. If you have chosen commit=1, so test "\ def do_action(update):
"going to be resolved in about 20 minutes and will "\ try:
"be commited automatically, otherwise it will take "\ s = update.message.text.split()
"about a 2 minutes and you have to "\ except:
"commit it by yourself. Just wait. PS you have "\ return 1
"chosen subj %s "\ if s[0] == '/resolve':
"test %s and accuracy %s" % (s[3], s[4], s[5]) if len(s) != 7:
update.message.reply_text(msg) update.message.reply_markdown("Missing operand... Try again")
update.message.reply_photo(open('1516736368795.png', 'rb')) update.message.reply_markdown("Usage: */resolve <user[text]> "\
perdoliq(s[1], s[2], s[3], s[4], s[5], s[6]) "<pass[text]> <subj[int]> <test[int]> "\
update.message.reply_text("It's done. Check your test because "\ "<accuracy[0-100]> <commit[1/0]>*")
"i disclaim any responsibility.") return False
elif s[0] == '/list':
try: msg = "Please wait. If you have chosen commit=1, so test "\
if len(s) == 3: "going to be resolved in about 20 minutes and will "\
update.message.reply_text("Fetching tests...") "be commited automatically, otherwise it will take "\
tests = list_test(s[1], s[2]) "about a 2 minutes and you have to "\
else: "commit it by yourself. Just wait. PS you have "\
update.message.reply_markdown("Usage: */list <user[text]>"\ "chosen subj %s "\
" <pass[text]>*") "test %s and accuracy %s" % (s[3], s[4], s[5])
return False update.message.reply_text(msg)
except: update.message.reply_photo(open('1516736368795.png', 'rb'))
update.message.reply_markdown("Usage: */list <user[text]>"\ perdoliq(s[1], s[2], s[3], s[4], s[5], s[6])
" <pass[text]>*") update.message.reply_text("It's done. Check your test because "\
return False "i disclaim any responsibility.")
msg = "Here is an available tests:\n``` " elif s[0] == '/list':
i = 0 try:
for subj in tests: if len(s) == 3:
msg = msg + (" [%s] %s\n" % (i, subj)) update.message.reply_text("Fetching tests...")
i += 1 sleep(5)
j = 0 tests = list_test(s[1], s[2])
for test in tests[subj]:
msg = msg + (" [%s] %s\n" % (j, test))
j += 1
update.message.reply_markdown(msg + "```\n Pay attention to "\
"numbers in brackets \[..] *Here is subj and test numbers*")
else: else:
update.message.reply_markdown("Possible commands: */resolve, /list*") update.message.reply_markdown("Usage: */list <user[text]>"\
update.message.reply_markdown("Usage: */resolve <user[text]> "\ " <pass[text]>*")
"<pass[text]> <subj[int]> <test[int]> "\ return False
"<accuracy[0-100]> <commit[1/0]>*") except:
update.message.reply_markdown("Usage: */list <user[text]> "\ update.message.reply_markdown("Usage: */list <user[text]>"\
"<pass[text]>*") " <pass[text]>*")
return False
msg = "Here is an available tests:\n``` "
i = 0
for subj in tests:
msg = msg + (" [%s] %s\n" % (i, subj))
i += 1
j = 0
for test in tests[subj]:
msg = msg + (" [%s] %s\n" % (j, test))
j += 1
update.message.reply_markdown(msg + "```\n Pay attention to "\
"numbers in brackets \[..] *Here is subj and test numbers*")
else:
update.message.reply_markdown("Possible commands: */resolve, /list*")
update.message.reply_markdown("Usage: */resolve <user[text]> "\
"<pass[text]> <subj[int]> <test[int]> "\
"<accuracy[0-100]> <commit[1/0]>*")
update.message.reply_markdown("Usage: */list <user[text]> "\
"<pass[text]>*")
if __name__ == '__main__': if __name__ == '__main__':
main() main()