Hello everyone in this tutorial we will just pass by chatterbot's place and say hi
First things first: Import libraries
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
This lines will import the Chatbot class and ListTrainer which will be used to train the bot.
Create a bot
chatbot = ChatBot("Adam")
Get a response
response = chatbot.get_response("Good morning!")
Whole code
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
chatbot = ChatBot("Adam")
response = chatbot.get_response("Good morning!")
I showed you guys some very simple snippets of fbchat and chatterbot. Its time to
integrate the two
Chatterbot + FBChat
So every-time someone sends us a message we want to send it to chatbot then send then then response back to the user. So how we do that?
Import libraries
from fbchat import log, Client
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from fb_creds import user_name, user_pass
import pickle
fb_creds is file where are creds are being stored. The pickle library will be used to store and retrieve session cookies.
Creating the bot
chatbot = ChatBot("Adam")
Behold the new onMessage
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
self.markAsDelivered(thread_id, message_object.uid)
self.markAsRead(thread_id)
# If you're not the author, echo
if author_id != self.uid:
# LINES FOR ECHOING
response = chatbot.get_response(message_object.text)
message_object.text = response
self.send(message_object, thread_id=thread_id, thread_type=thread_type)
So now we are now making use of message_object to get the text message, then pass it to our chatbot. From there we change message_object text property to whatever is returned by the chatbot then we send it back to the sender.
WE ARE SMART RIGHT?
Nuh your bot is still dum. Your bot cant get you any new girlfriends while you sleep.πππ
Bye Bye
Top comments (0)