-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleAPI.py
59 lines (46 loc) · 1.37 KB
/
ConsoleAPI.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
from API import *
#import telegram
#TODO Set target API from config
import asyncio
from aioconsole import ainput, aprint
import aconsole
import threading
import time
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class CLIBotWrapper(API_Wrapper):
def __init__(self, *args, **kwards):
self.received_messages = []
self.messages_to_send = []
self.console_t = threading.Thread(target=self.async_console_prcessor)
self.console_t.start()
def check_last_update(self):
if self.received_messages:
return self.received_messages.pop()
return ""
def send_update(self, update_message):
print(bcolors.OKGREEN + update_message + bcolors.ENDC)
self.messages_to_send.append(update_message)
async def check_updates(self):
while True:
console_phrase = await ainput('')
self.received_messages.append(console_phrase)
async def send_updates(self):
while True:
if not self.messages_to_send:
time.sleep(2)
continue
await aprint(bcolors.OKGREEN + self.messages_to_send.pop() + bcolors.ENDC)
def async_console_prcessor(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.create_task(self.check_updates())
loop.run_forever()