-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBotApi.py
186 lines (157 loc) · 8.06 KB
/
BotApi.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- Alex -*-
import urllib2
import urllib
import json
import requests
from Colors import Bcolors
from SystemUtils import SystemUtils
import os
import time
import threading
__version__ = '1.0.1'
class BotApi(object):
def __init__(self):
self.json_response = None
bot_token = 'BOT TOKEN HERE'
api_url = 'https://api.telegram.org/bot'
full_url = api_url + bot_token
status_json = '/getUpdates'
send_message_method = '/sendMessage'
update_url = full_url + status_json
user_id = 718943
load_command = "/load"
info_command = "/info"
help_command = "/help"
mem_commmand = "/memory"
apache_restart = "/apache_restart"
mysql_restart = "/mysql_restart"
check_site_status = "/check_site_status"
def send_message(self, command, information):
help_message = "Yon asked for /help"
error_message = "Wrong command!"
if command == BotApi.load_command:
http_post = requests.post(BotApi.full_url + self.send_message_method, data={'chat_id': 718943, 'text': str(information)})
print Bcolors.OKGREEN + "Response from Web-Server: \n" + Bcolors.ENDC + str((http_post.status_code, http_post.reason))
elif command == BotApi.info_command:
http_post = requests.post(BotApi.full_url + self.send_message_method, data={'chat_id': 718943, 'text': str(information)})
print Bcolors.OKGREEN + "Response from Web-Server: \n" + Bcolors.ENDC + str((http_post.status_code, http_post.reason))
elif command == BotApi.help_command:
http_post = requests.post(BotApi.full_url + self.send_message_method, data={'chat_id': 718943, 'text': help_message})
print Bcolors.OKGREEN + "Response from Web-Server: \n" + Bcolors.ENDC + str((http_post.status_code, http_post.reason))
elif command == BotApi.mem_commmand:
http_post = requests.post(BotApi.full_url + self.send_message_method, data={'chat_id': 718943, 'text': str(information)})
print Bcolors.OKGREEN + "Response from Web-Server: \n" + Bcolors.ENDC + str((http_post.status_code, http_post.reason))
elif command == BotApi.apache_restart:
http_post = requests.post(BotApi.full_url + self.send_message_method, data={'chat_id': 718943, 'text': str(information)})
print Bcolors.OKGREEN + "Response from Web-Server: \n" + Bcolors.ENDC + str((http_post.status_code, http_post.reason))
elif command == BotApi.check_site_status:
http_post = requests.post(BotApi.full_url + self.send_message_method, data={'chat_id': 718943, 'text': str(information)})
print Bcolors.OKGREEN + "Response from Web-Server: \n" + Bcolors.ENDC + str((http_post.status_code, http_post.reason))
elif command == BotApi.mysql_restart:
http_post = requests.post(BotApi.full_url + self.send_message_method, data={'chat_id': 718943, 'text': str(information)})
print Bcolors.OKGREEN + "Response from Web-Server: \n" + Bcolors.ENDC + str((http_post.status_code, http_post.reason))
else:
http_post = requests.post(BotApi.full_url + self.send_message_method, data={'chat_id': 718943, 'text': str(error_message)})
print Bcolors.OKGREEN + "Response from Web-Server: \n" + Bcolors.ENDC + str((http_post.status_code, http_post.reason)) + "\n"
def get_command(self):
path_to_file = os.getcwd()
file_with_id = path_to_file + "/update_id_file.txt"
with open(file_with_id, 'r') as myfile:
last_id = myfile.read()
myfile.close()
offset = int(last_id)
offset = int(offset) - 1
values = {}
values['offset'] = offset
values['limit'] = '5'
data = urllib.urlencode(values)
response = urllib2.Request(self.update_url, data)
response = urllib2.urlopen(response)
block_from_json_response = response.read()
self.json_response = json.loads(block_from_json_response)
last_update_id_from_json = self.json_response['result'][-1]['update_id']
last_user_id = self.json_response['result'][-1]['message']['from']['id']
update_id_array = []
for key in self.json_response['result']:
update_id_array.append(key['update_id'])
path_to_file = os.getcwd()
file_with_id = path_to_file + "/update_id_file.txt"
if os.path.exists(file_with_id):
with open(file_with_id, 'r+') as myfile:
last_update_id = myfile.read()
if int(last_update_id) == int(last_update_id_from_json):
myfile.close()
else:
myfile.read()
myfile.seek(0)
myfile.write(str(last_update_id_from_json))
myfile.close()
command = self.json_response['result'][-1]['message']['text']
system = SystemUtils()
if last_user_id == self.user_id:
if command == BotApi.info_command:
inf = system.get_info_from_server(command)
information = inf.to_string()
self.send_message(command, information)
elif command == BotApi.help_command:
self.send_message(command, '')
elif command == BotApi.load_command:
load = system.get_load_from_from_server(command)
self.send_message(command, load)
elif command == BotApi.mem_commmand:
mem = system.get_memory(command)
self.send_message(command, mem)
elif command == BotApi.apache_restart:
restart = system.restart_apache(command)
self.send_message(command, restart)
elif command == BotApi.mysql_restart:
restart = system.restart_mysqld(command)
self.send_message(command, restart)
elif command == BotApi.check_site_status:
online_status = system.get_status_online(command)
self.send_message(command, online_status)
else:
self.send_message(command, '')
print Bcolors.OKGREEN + "Nothing to send, error command has been entered" + Bcolors.ENDC
else:
self.detect_wrong_user()
else:
print "File: " + file_with_id + " doesn't exist"
def get_automatic_site_status(self):
status_huyatus = SystemUtils()
while 1:
try:
status_huyatus.get_automatic_site_status()
time.sleep(5)
except Exception, e:
time.sleep(5)
print str(e)
def engine(self):
while 1:
try:
self.get_command()
time.sleep(1)
except Exception, e:
time.sleep(5)
print str(e)
def detect_wrong_user(self):
values = {}
values['chat_id'] = self.json_response['result'][-1]['message']['chat']['id']
first_name_wrong_user = self.json_response['result'][-1]['message']['chat']['first_name']
last_name_wrong_user = self.json_response['result'][-1]['message']['chat']['last_name']
values['text'] = "Hello " + first_name_wrong_user + " " + last_name_wrong_user + " you are not authorized to send commands"
http_post = requests.post(BotApi.full_url + self.send_message_method, data=values)
def run_thread1(self):
thread = threading.Thread(target=self.get_automatic_site_status)
thread.start()
def run_thread2(self):
thread = threading.Thread(target=self.engine)
thread.start()
if __name__ == '__main__':
print Bcolors.OKGREEN + "Starting bot..." + Bcolors.ENDC
obj = BotApi()
obj.run_thread1()
obj1 = BotApi()
obj1.run_thread2()