-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwechat.py
51 lines (38 loc) · 1.64 KB
/
wechat.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
import threading
import queue
import time
import itchat
import client_control
class WechatController(client_control.ClientController):
def __init__(self, game_controller):
super().__init__(game_controller)
self.username_to_user = {} # Map Wechat user name to WechatUser object
self.send_msg_queue = queue.Queue() # Avoid sending messages too fast by buffering
# Start listening Wechat messages
itchat.auto_login()
threading.Thread(target = itchat.run).start()
# Send messages in another thread
threading.Thread(target = self.send_messages_in_queue).start()
# Accept new messages from players
@itchat.msg_register(itchat.content.TEXT)
def listen_wechat_message(message_info):
username = message_info['User']['UserName'] # User name of the Wechat user
text = message_info['Text'] # Content of the message
self.got_message(username, text)
def send_messages_in_queue(self):
while(True):
(username, message) = self.send_msg_queue.get()
itchat.send(message, toUserName = username)
time.sleep(0.5)
def register_user(self, username, user):
self.username_to_user[username] = user
def user_from_username(self, username):
try:
return self.username_to_user[username]
except KeyError:
raise client_control.UserNotRegistered()
def clear_screen(self, username):
message = '\n'*25 + '清屏'
self.send_message(username, message)
def send_message(self, username, message):
self.send_msg_queue.put((username, message))