-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinfo.py
197 lines (163 loc) · 12.6 KB
/
info.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
187
188
189
190
191
192
193
194
195
196
197
import re
from zlapi import ZaloAPI, ZaloAPIException
from zlapi.models import *
from datetime import datetime
class Client(ZaloAPI):
def __init__(self, api_key, secret_key, imei, session_cookies):
super().__init__(api_key, secret_key, imei=imei, session_cookies=session_cookies)
def onMessage(self, mid, author_id, message, message_object, thread_id, thread_type):
print(f"Received message: {message} from {author_id} in thread {thread_id}, type {thread_type}, {message_object}")
if not isinstance(message, str):
print(f"Unexpected message type: {type(message)}")
return
if message.startswith(".info"):
msg_error = f"🔴 Something went wrong\n| Không thể lấy thông tin tài khoản Zalo!"
style_error = MultiMsgStyle([
MessageStyle(offset=0, length=23, style="font", size="12", auto_format=False),
MessageStyle(offset=2, length=21, style="color", color="#f38ba8", auto_format=False),
MessageStyle(offset=24, length=1, style="color", color="#585b70", auto_format=False),
MessageStyle(offset=24, length=1, style="bold", auto_format=False),
MessageStyle(offset=25, length=len(msg_error.encode()), style="font", size="13", auto_format=False),
MessageStyle(offset=25, length=len(msg_error.encode()), style="color", color="#cdd6f4", auto_format=False)
])
try:
if message_object.mentions:
user_id = message_object.mentions[0]['uid']
elif message[5:].strip().isnumeric():
user_id = message[5:].strip()
elif message.strip() == ".info":
user_id = author_id
else:
self.send(Message(text=msg_error, style=style_error), thread_id, thread_type)
return
msg = ""
multistyle = []
try:
info = self.fetchUserInfo(user_id)
info = info.unchanged_profiles or info.changed_profiles
if info is self._undefined:
self.send(Message(text=msg_error, style=style_error), thread_id, thread_type)
return
info = info[str(user_id)]
userId = info.userId or "Undefined"
msg += f"• User ID: {userId}\n"
offset = self.count(msg, "•")[0]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• User ID: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 11, length=length2, style="color", color="#a6adc8", auto_format=False))
userName = info.zaloName[:30] + "..." if len(info.zaloName) > 30 else info.zaloName
userName = self.remove_special_chars(userName)
msg += f"• User Name: {userName}\n"
offset = self.count(msg, "•")[1]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• User Name: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 13, length=length2, style="color", color="#a6adc8", auto_format=False))
gender = "Male" if info.gender == 0 else "Female" if info.gender == 1 else "Undefined"
msg += f"• Gender: {gender}\n"
offset = self.count(msg, "•")[2]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• Gender: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 10, length=length2, style="color", color="#a6adc8", auto_format=False))
status = self.remove_special_chars(info.status) or "Default"
msg += f"• Bio: {status}\n"
offset = self.count(msg, "•")[3]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• Bio: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 7, length=length2, style="color", color="#a6adc8", auto_format=False))
business = info.bizPkg.label
business = "Yes" if business else "No"
msg += f"• Business: {business}\n"
offset = self.count(msg, "•")[4]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• Business: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 12, length=length2, style="color", color="#a6adc8", auto_format=False))
dob = info.dob or info.sdob or "Hidden"
if isinstance(dob, int):
dob = datetime.fromtimestamp(dob).strftime("%d/%m/%Y")
msg += f"• Date Of Birth: {dob}\n"
offset = self.count(msg, "•")[5]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• Date Of Birth: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 17, length=length2, style="color", color="#a6adc8", auto_format=False))
phoneNumber = info.phoneNumber or "Hidden"
msg += f"• Phone Number: {phoneNumber}\n"
offset = self.count(msg, "•")[6]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• Phone Number: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 10, length=length2, style="color", color="#a6adc8", auto_format=False))
# Adding Last Action Time
lastAction = info.lastActionTime
if isinstance(lastAction, int):
lastAction = lastAction / 1000
timeAction = datetime.fromtimestamp(lastAction)
lastAction = timeAction.strftime("%H:%M %d/%m/%Y")
else:
lastAction = "Undefined"
msg += f"• Last Action At: {lastAction}\n"
offset = self.count(msg, "•")[7]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• Last Action At: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 18, length=length2, style="color", color="#a6adc8", auto_format=False))
# Adding Created Time
createTime = info.createdTs
if isinstance(createTime, int):
createTime = datetime.fromtimestamp(createTime).strftime("%H:%M %d/%m/%Y")
else:
createTime = "Undefined"
msg += f"• Created Time: {createTime}\n"
offset = self.count(msg, "•")[8]
length = len(msg.rsplit("• ")[-1:][0].strip())
length2 = len(msg.split("• Created Time: ")[1].strip())
multistyle.append(MessageStyle(offset=offset, length=1, style="color", color="#74c7ec", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="font", size="13", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 2, length=length, style="color", color="#cdd6f4", auto_format=False))
multistyle.append(MessageStyle(offset=offset + 16, length=length2, style="color", color="#a6adc8", auto_format=False))
msg_to_send = Message(text=msg, style=MultiMsgStyle(multistyle))
self.replyMessage(msg_to_send, message_object, thread_id, thread_type)
except ZaloAPIException as e:
print(f"Error fetching user info: {e}")
self.send(Message(text=msg_error, style=style_error), thread_id, thread_type)
except Exception as e:
print(f"Error: {e}")
self.send(Message(text=msg_error, style=style_error), thread_id, thread_type)
def remove_special_chars(self, string):
return re.sub(r'[^a-zA-Z0-9-\sàáãạảăắằẳẵặâấầẩẫậèéẹẻẽêềếểễệìíịỉĩòóõọỏôốồổỗộơớờởỡợùúũụủưứừửữựỳýỵỷỹÀÁÃẠẢĂẮẰẲẴẶÂẤẦẨẪẬÈÉẸẺẼÊỀẾỂỄỆÌÍỊỈĨÒÓÕỌỎÔỐỒỔỖỘƠỚỜỞỠỢÙÚŨỤỦƯỨỪỬỮỰỲÝỴỶỸ]', '', string)
def count(self, string: str, word: str) -> list:
indices = []
start = 0
while True:
idx = string.find(word, start)
if idx == -1:
break
indices.append(idx)
start = idx + 1
return indices
imei = ""
session_cookies = {
}
client = Client('api_key', 'secret_key', imei=imei, session_cookies=session_cookies)
client.listen()