-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
119 lines (93 loc) · 3.16 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
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
# coding: utf-8
# In[31]:
import time
import random
import datetime
import telepot
from telepot.loop import MessageLoop
import sys
import glob
import importlib.util
import re
#TELEGRAM BOT PARAMS
TOKEN = ''
BOTNAME = ''
# Get file paths of all modules.
MODULES_PATH = glob.glob('modules/*.py')
def handle(msg):
parse_mode = None
chat_id = msg['chat']['id']
#print msg['text']
a_text = msg['text'].split(" ")
command = a_text[0]
if a_text[0] == BOTNAME:
a_text.pop(0)
if len(a_text) > 0:
command = a_text[0]
print('Got command: %s' % command)
msg = ""
found_bool = False
#check command name
for module in all_commands.keys():
if all_commands[module]['commands'] != None:
if command in all_commands[module]['commands'].keys():
a_text.pop(0)
if 'parse_mode' in all_commands[module]['commands'][command]:
parse_mode = all_commands[module]['commands'][command]['parse_mode']
else:
parse_mode = None
spec = importlib.util.spec_from_file_location(all_commands[module]['path'],module)
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
msg = foo.exec_my_commands(command,a_text)
found_bool = True
break
if(not found_bool):
msg = "You can ask me:\n"
for module in all_commands.keys():
if all_commands[module]['commands'] != None:
for command in all_commands[module]['commands'].keys():
msg = str(msg) + str(command)+": "+ all_commands[module]['commands'][command]["notes"]+ "\n"
if len(all_commands[module]['commands'].keys()) != 0:
msg = msg + "\n"
list_msgs = []
original_length = len(msg)
while len(msg) > 3000:
index = 3000
while True:
if (msg[index:index+1] == "\n"):
break
else:
index -= 1
list_msgs.append(msg[0:index])
msg = msg[index:original_length-1]
list_msgs.append(msg)
#print("send back: "+str(len(list_msgs))+" msgs.")
#print(list_msgs[len(list_msgs)-1])
count_msgs = 0
for m in list_msgs:
#print(m[0:10].encode())
#print(m[-1].encode())
if count_msgs >= 3:
bot.sendMessage(chat_id,'Too many messages to send !',parse_mode= None,disable_web_page_preview=True)
break
bot.sendMessage(chat_id,m,parse_mode= parse_mode,disable_web_page_preview=True)
count_msgs += 1
time.sleep(0.5)
all_commands = {}
for m in MODULES_PATH:
url = 'modules/'
m_name = re.sub('modules/', '', m)
m_name = re.sub('.py','', m_name)
spec = importlib.util.spec_from_file_location(m_name, m)
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
all_commands[m] = {}
all_commands[m]['path'] = m_name
all_commands[m]['commands'] = foo.get_my_commands()
bot = telepot.Bot(TOKEN)
MessageLoop(bot, handle).run_as_thread()
print('I am listening ...')
while 1:
time.sleep(10)
# In[ ]: