Last active
September 6, 2017 07:06
-
-
Save hanguyen221/e5cd7310da6ead4bb2a9d52c9996f4bc to your computer and use it in GitHub Desktop.
Chat bot for facebook messenger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
import os.path | |
sys.path.append('/usr/local/lib/python3.6/site-packages') | |
import fbchat | |
from fbchat import log, Client # Nạp thư viện của fbchat | |
from fbchat.models import TypingStatus | |
import re | |
import time | |
class EchoBot(Client): | |
def onMessage(self, author_id, message, thread_id, thread_type, **kwargs): | |
# self.markAsDelivered(author_id, thread_id) # Đánh dấu đã nhận tin nhắn | |
# self.markAsRead(author_id) # Đánh dấu đã đọc tin nhắn | |
# Hiển thị log mỗi khi có tin nhắn tới | |
log.info("Message from {} in {} ({}): {}".format(author_id, thread_id, thread_type.name, message)) | |
#Flag để bật/tắt bot: flag = 1 thì bot sẽ rep còn flag = 0 thì sẽ không rep nữa (tức là lúc đấy mình đang onl rồi) | |
if message == "A đây rồi e out đi": | |
self.sendMessage('Dạ anh', thread_id=thread_id, thread_type=thread_type) | |
self.flag = 0 | |
elif message == "Nói chuyện vs ny t nhé": | |
self.sendMessage('Hi ^^', thread_id=thread_id, thread_type=thread_type) | |
self.flag = 1 | |
elif message == "E log out đi": | |
self.sendMessage('Dạ anh', thread_id=thread_id, thread_type=thread_type) | |
self.logout() | |
time.sleep(3) | |
# onMessage sẽ bắt cả tin nhắn gửi đi và gửi đến | |
# Nếu id của tác giả tin nhắn không phải là bạn thì | |
if author_id not in self.ingoreIds and self.flag == 1 and thread_type.value == 1: | |
if author_id != self.partnerId: | |
self.index = 0 | |
self.partnerId = author_id | |
# Gửi tin nhắn cho cuộc hội thoại có id là thread_id, tin nhắn lấy từ 1 dãy tạo trước | |
pattern1 = r"(vl|vlone|vailone|vãi|vai|vc|loz)" | |
pattern2 = r"(đm|đcm|dm|dcm|địt|đ!t|dis|đù|du)" | |
pattern3 = r"bot" | |
pattern4 = r"tin" | |
pattern5 = r"^(\.)+$" | |
pattern6 = r"gì|clgt|clgv" | |
pattern7 = r"thật" | |
pattern8 = r"@@" | |
pattern9 = r"đc|được|dc|duoc" | |
pattern10 = r"^(\:|\=)(\))+$" | |
if re.search(pattern1, message, re.IGNORECASE): | |
self.sendMessage("Gì mà {} vậy Hà chưa nói vs bạn về mình à =))".format(re.search(pattern1, message, re.IGNORECASE).group()), thread_id=thread_id, thread_type=thread_type) | |
elif re.search(pattern2, message, re.IGNORECASE): | |
self.sendMessage("Ui nói bậy quá nè ^^", thread_id=thread_id, thread_type=thread_type) | |
elif re.search(pattern3, message, re.IGNORECASE): | |
self.sendMessage("Chời đâu phải bot đâu người thật mà", thread_id=thread_id, thread_type=thread_type) | |
elif re.search(pattern4, message, re.IGNORECASE): | |
self.sendMessage("Ko tin thì hoy hihi", thread_id=thread_id, thread_type=thread_type) | |
elif re.match(pattern5, message, re.IGNORECASE): | |
self.sendMessage("Sao mà chấm chấm vậy ^^", thread_id=thread_id, thread_type=thread_type) | |
elif re.search(pattern6, message, re.IGNORECASE): | |
self.sendMessage("Ny của Hà chứ gì", thread_id=thread_id, thread_type=thread_type) | |
elif re.search(pattern7, message, re.IGNORECASE): | |
self.sendMessage("Thật chứ sao ko hihi", thread_id=thread_id, thread_type=thread_type) | |
elif re.search(pattern8, message): | |
self.sendMessage("Ủa sao vậy h mới biết à :D", thread_id=thread_id, thread_type=thread_type) | |
elif re.search(pattern9, message, re.IGNORECASE): | |
self.sendMessage("Được chứ sao ko =))", thread_id=thread_id, thread_type=thread_type) | |
elif re.search(pattern10, message, re.IGNORECASE): | |
self.sendMessage("Cười púa ý ^^", thread_id=thread_id, thread_type=thread_type) | |
else: | |
self.sendMessage(self.messages[self.index], thread_id=thread_id, thread_type=thread_type) | |
self.index += 1 | |
log.info("Index now is {}".format(self.index)) | |
if self.index == len(self.messages): | |
self.index = 0 | |
client = EchoBot("email@gmail.com", "password", max_tries=10) # Điền email, password tài khoản Facebook của bạn vô đây | |
client.flag = 1 | |
client.index = 0 | |
client.partnerId = 0 | |
client.ingoreIds = [client.uid, str(100002626520732), str(100013869668583)] | |
client.messages = ['Mình là ny của Hà, hiện tại Hà đang bận việc xíu xong việc Hà sẽ rep bạn ngay nha ^^', 'Bạn chịu khó đợi xíu nha', 'Đừng hỏi gì mình hong bít gì đâu ^^', '5 phút nữa thôi bạn ^^', 'Hà sắp xong việc rồi nè', 'Một chút nữa thôi mà'] | |
client.listen() # Bot sẽ chạy cập nhật tin nhắn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment