-
Notifications
You must be signed in to change notification settings - Fork 0
/
botchat.py
109 lines (90 loc) · 4.75 KB
/
botchat.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
# -*- coding: UTF-8 -*-
from fbchat import log, Client
from fbchat.models import *
from sendmail import SendMail
import getpass
# Subclass fbchat.Client and override required methods
class Run_App(Client):
action = 1
def onMessage(self, mid, author_id, message, message_object, thread_id, thread_type, **kwargs):
"""
Dung de bat su kien khi co tin nhan moi!
"""
print(thread_id)
self.markAsDelivered(author_id, thread_id)
self.markAsRead(author_id)
# if message_object.text == 'Talk with strangers!':
# self.action = 2
if author_id == self.uid:
if message_object.text == 'Stop!':
self.action = 0
message_relay = 'Bot: Đã tiếp nhận yêu cầu!!'
relay = Message(text = message_relay)
self.send(relay, thread_id=thread_id, thread_type=thread_type)
if message_object.text == 'Start!':
self.action = 1
message_relay = 'Bot: Đã tiếp nhận yêu cầu!!'
relay = Message(text = message_relay)
self.send(relay, thread_id=thread_id, thread_type=thread_type)
if self.action == 0:
return
else:
log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name))
# If you're not the author, echo
if author_id != self.uid:
user = self.fetchUserInfo(author_id)[author_id]
# Đánh dấu “Yêu thích” tin nhắn người gửi
self.reactToMessage(mid, MessageReaction.LOVE)
# message_relay = 'Vui lòng chờ trong giây lát!'
# relay = Message(text = message_relay)
# self.send(relay, thread_id=thread_id, thread_type=thread_type)
# message_relay = 'Thông tin người gửi: {}\n'.format(user.name)
# relay = Message(text = message_relay)
# self.send(relay, thread_id=thread_id, thread_type=thread_type)
# message_relay = 'Mã người gửi: {}\n'.format(user.uid)
# relay = Message(text = message_relay)
# self.send(relay, thread_id=thread_id, thread_type=thread_type)
# message_relay = 'Hình ảnh người gửi: {}'.format(user.photo)
# relay = Message(text = message_relay)
# self.send(relay, thread_id=thread_id, thread_type=thread_type)
# message_relay = 'Có phải bạn của chủ nhân: {}\n'.format(user.is_friend)
# relay = Message(text = message_relay)
# self.send(relay, thread_id=thread_id, thread_type=thread_type)
# message_relay = 'Giới tính: {}\n'.format(user.gender)
# relay = Message(text = message_relay)
# self.send(relay, thread_id=thread_id, thread_type=thread_type)
# message_relay = 'Địa chỉ profile: {}\n'.format(user.url)
# relay = Message(text = message_relay)
# self.send(relay, thread_id=thread_id, thread_type=thread_type)
message_relay = 'Bot: Đã nhận được tin nhắn từ "{}" với nội dung là: "{}"'.format(self.fetchThreadInfo(thread_id)[thread_id].name, message)
relay = Message(text = message_relay)
self.send(relay, thread_id=thread_id, thread_type=thread_type)
message_relay = 'Bot: Đang tiến hành gửi mail!'
relay = Message(text = message_relay)
self.send(relay, thread_id=thread_id, thread_type=thread_type)
# Gửi mail
send_mail = SendMail
email_from = 'dinhtruong018@gmail.com'
password = 'XXX'
email_to = 'XXX@gmail.com'
try:
send_mail.Send(email_from, password, email_to, user.name, message)
message_relay = """
Bot: Thư đã được gửi đến Peter.
Đừng đi đâu hết!
Peter sẽ trả lời bạn ngay ~~
"""
relay = Message(text = message_relay)
self.send(relay, thread_id=thread_id, thread_type=thread_type)
except:
message_relay = """
Bot: Có lỗi khi gửi mail!
Xin lỗi!
"""
relay = Message(text = message_relay)
self.send(relay, thread_id=thread_id, thread_type=thread_type)
if __name__ == '__main__':
username = input("Username: ")
password = getpass.getpass("Password: ")
client = Run_App(username,password)
client.listen()