-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (63 loc) · 2.58 KB
/
main.py
File metadata and controls
81 lines (63 loc) · 2.58 KB
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
import getpass
from termcolor import colored
from pusher import Pusher
import pysher
from dotenv import load_dotenv
import os
import json
load_dotenv(dotenv_path='.env')
class terminalChat():
pusher = None
channel = None
chatroom = None
clientPusher = None
user = None
users = {
"user1": "user1'spassword",
"user2": "user2'spassword"
}
chatrooms = ["general"]
def main(self):
self.login()
self.selectChatroom()
while True:
self.getInput()
def login(self):
username = input("Please enter your username: ")
password = getpass.getpass("Please enter %s's Password:" % username)
if username in self.users:
if self.users[username] == password:
self.user = username
else:
print(colored("Your password is incorrect", "red"))
self.login()
else:
print(colored("Your username is incorrect", "red"))
self.login()
def selectChatroom(self):
print(colored("Info! Available chatrooms are %s" % str(self.chatrooms), "blue"))
chatroom = input(colored("Please select a chatroom: ", "green"))
if chatroom in self.chatrooms:
self.chatroom = chatroom
self.initPusher()
else:
print(colored("No such chatroom in our list", "red"))
self.selectChatroom()
def initPusher(self):
self.pusher = Pusher(app_id=os.getenv('PUSHER_APP_ID', None), key=os.getenv('PUSHER_APP_KEY', None), secret=os.getenv('PUSHER_APP_SECRET', None), cluster=os.getenv('PUSHER_APP_CLUSTER', None))
self.clientPusher = pysher.Pusher(os.getenv('PUSHER_APP_KEY', None), os.getenv('PUSHER_APP_CLUSTER', None))
self.clientPusher.connection.bind('pusher:connection_established', self.connectHandler)
self.clientPusher.connect()
def connectHandler(self, data):
self.channel = self.clientPusher.subscribe(self.chatroom)
self.channel.bind('newmessage', self.pusherCallback)
def pusherCallback(self, message):
message = json.loads(message)
if message['user'] != self.user:
print(colored("{}: {}".format(message['user'], message['message']), "blue"))
print(colored("{}: ".format(self.user), "green"))
def getInput(self):
message = input(colored("{}: ".format(self.user), "green"))
self.pusher.trigger(self.chatroom, u'newmessage', {"user": self.user, "message": message})
if __name__ == "__main__":
terminalChat().main()