-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.py
129 lines (99 loc) · 4.72 KB
/
controller.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
120
121
122
123
124
125
126
127
128
129
import fileManage
import botfunctions
import webScraping
import requests
import string
import time
import os
import subprocess
import time
import user
botName = "Thanos"
localBotName = ""
#GETS THE SENSITVE BOT INFO
def botinfo():
global botID
global uID
global groupID
global first_run
first_run = True
f = open("pass.txt", "r")
lines = [line.rstrip('\n') for line in f]
botID = lines[0]
uID = lines[1]
groupID = lines[2]
#SENDS A MESSAGE TO THE GROUP
def send(msg):
post_params = {'bot_id': botID, 'text': msg}
requests.post('https://api.groupme.com/v3/bots/post', params=post_params)
return;
#CHECKS ALL THE MESSAGES FOR KEYWORDS
def parse_messages(valid_messages):
for message in valid_messages:
print(message['text'])
text = message['text'].lower().rstrip()
if('echo' == text[0:4] and user.allowed('echo', groupdata[message['name']])):
send(botfunctions.echo(message))
if('whois' == text and user.allowed('whois', groupdata[message['name']])):
send(os.getlogin())
if('exec' == text[0:4] and user.allowed('exec', groupdata[message['name']])):
send(botfunctions.exec(message))
# regular stop
if (text == 'exit' and not first_run and user.allowed('exit', groupdata[message['name']])):
print('ended')
exit()
if ("weather" in text[0:7] and text != "weather" and user.allowed('weather', groupdata[message['name']])):
send(webScraping.getWeather(text[8:]))
if ("google" in message['text'].lower() and user.allowed('google', groupdata[message['name']])):
send(webScraping.letMeGoogleThatForYou(message['text']))
#sends entire message to lmgtfy function
if ('pycall' in message['text']):
send(botfunctions.runpython(message))
if('restart' in message['text']):
botfunctions.restart()
exit;
if ("help" in text[0:4]):
send(botfunctions.getHelp())
if ("promote" in message['text'].lower() and user.allowed('promote', groupdata[message['name']])):
msg = message['text'].split()
if (msg.index("promote") <= len(msg)-2):
if (msg.index("promote") == len(msg)-2 and msg[len(msg)-1] in groupdata.keys()):
send(groupdata[msg[len(msg)-1]].promotePlus(groupdata[message['name']]))
if (msg.index("promote") == len(msg)-3 and msg[len(msg)-2] in groupdata.keys()):
send(groupdata[msg[len(msg)-2]].promote(groupdata[message['name']], msg[len(msg)-1]))
#send(user.parsePromote(message['text'])
if ("users" == text and user.allowed('users', groupdata[message['name']])):
send(botfunctions.who(groupdata))
#main
if __name__ == '__main__':
botinfo() #fetch sensitive bot info
request_params = {'token': uID}
#if there is no recorded last run of time make the current time the last time read
if(not os.path.exists("time.txt")):
fileManage.writeFile('time.txt', str(0))
groupdata = user.assign(botID,uID, groupID)
while True:
response = requests.get('https://api.groupme.com/v3/groups/{}/messages'.format(groupID), params=request_params)
#Get the messages
if (response.status_code == 200):
response_messages = response.json()['response']['messages']
#get the last time messages were read and write the current time as the last time messages were read
last_time = int(fileManage.readFile("time.txt")) #LAST CONFIRMEND SECOND THAT ALL MESSAGES WERE READ
valid_messages = []
most_recent_message_time = 0
# Iterate through each message, checking that it is a message that is not read before and not sent by the bot
for message in response_messages:
if(message['name'] != botName and most_recent_message_time < message['created_at']):
most_recent_message_time = message['created_at']
if(most_recent_message_time == last_time + 1):
most_recent_message_time += 1
for message in response_messages:
if(message['created_at'] > last_time and message['created_at'] < most_recent_message_time and message["name"] != botName):
valid_messages.append(message)
if(last_time == 0):
valid_messages = []
if(most_recent_message_time != 0 and most_recent_message_time != last_time):
fileManage.writeFile('time.txt', str(most_recent_message_time-1))
parse_messages(valid_messages)
first_run = False
time.sleep(1)