-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bot.py
95 lines (73 loc) · 2.52 KB
/
Bot.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
import asyncio
from mattermostdriver import Driver
import json
import sys
from config.Config import Config
from debug.ClientLogger import ClientLogger, LoggingMode
class Bot:
bot = None
host = None
port = None
https = None
username = None
passwd = None
driver = None
bot_info = None
def __init__(self, host, username, passwd, port=443, https=True):
Bot.bot = self
self.host = host
self.port = port
self.https = https
self.username = username
self.passwd = passwd
ClientLogger.log(LoggingMode.INFO, "Creating mettermost driver... ")
self.create_driver()
ClientLogger.log(LoggingMode.INFO, "Logging in... ")
self.login()
ClientLogger.log(LoggingMode.INFO, 'Successfully logged in!')
from connection.WebSocket import WebSocket
ClientLogger.log(LoggingMode.INFO, "Setting up web socket...")
websocket = WebSocket()
ClientLogger.log(LoggingMode.INFO, "Listening for user commands...")
asyncio.get_event_loop().run_until_complete(websocket.listen())
@classmethod
def get_bot(cls):
return Bot.bot
def create_driver(self):
self.driver = Driver({
'url': self.host,
'login_id': self.username,
'password': self.passwd,
'scheme': 'https' if self.https else 'http',
'port': self.port
})
def login(self):
self.driver.login()
self.bot_info = self.driver.users.get_user_by_username(Config.get_user())
def logout(self):
self.driver.logout()
def kill(self):
self.logout()
sys.exit()
def get_bot_id(self):
return self.get_id_from_json_data(self.bot_info)
def get_bot_channel_id(self):
return self.get_id_from_json_data(
self.driver.channels.get_channel_by_name_and_team_name(Config.get_team(), Config.get_channel()))
def create_message(self, text):
self.driver.posts.create_post({
'user_id': self.get_bot_id(),
'channel_id': self.get_bot_channel_id(),
'message': text
})
def get_all_unread_messages(self):
return self.driver.channels.get_unread_messages(self.get_bot_id(), self.get_bot_channel_id())
def get_auth_token(self):
return self.driver.users.get_au
@staticmethod
def get_team_name():
return Config.get_team()
@staticmethod
def get_id_from_json_data(data):
json.loads(json.dumps(data))
return data['id']