-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (44 loc) · 1.77 KB
/
main.py
File metadata and controls
61 lines (44 loc) · 1.77 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
from pytg.sender import Sender
from pytg.receiver import Receiver
from pytg.utils import coroutine
from AdminCommands import AdminCommands
from RuleManager import RuleManager
import logging
logging.basicConfig(filename='runtime_log.log', format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.info("Running start")
receiver = Receiver(host="localhost", port=4458)
sender = Sender(host="localhost", port=4458)
admin = sender.get_self()
adminCommands = AdminCommands(admin)
print("Got: >%s<" % admin)
username = admin.username
print("my username is {user}".format(user=username))
sender.send_msg(admin['id'], "Reenvio de mensajes condicional activado", enable_preview=True)
receiver.start()
ERROR_COUNT_LIMIT = 10
@coroutine
def example_function(receiver):
while True:
try:
telegram_msg = (yield)
# print('Full dump: {array}'.format(array=str(telegram_msg)))
# check for admin
if adminCommands.handle(telegram_msg):
continue
for rule in RuleManager.rules:
rule.execute(telegram_msg)
except KeyboardInterrupt:
receiver.stop()
break
except Exception as ex:
global ERROR_COUNT_LIMIT
logging.exception(ex, exc_info=True)
ERROR_COUNT_LIMIT -= 1
if ERROR_COUNT_LIMIT == 0:
sender.send_msg(admin['id'], "El programa se apagará por motivos de seguridad.")
raise ex
else:
sender.send_msg(admin['id'],
"Quedan " + str(ERROR_COUNT_LIMIT) + " errores antes de que se apague el programa.")
receiver.message(example_function(receiver))
sender.send_msg(admin['id'], "El reenvio de mensajes condicional está desactivado")