Skip to content

Instantly share code, notes, and snippets.

@sgoblin
Created December 22, 2016 21:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sgoblin/d05189bb31645a6c63e16d666bbce090 to your computer and use it in GitHub Desktop.
Save sgoblin/d05189bb31645a6c63e16d666bbce090 to your computer and use it in GitHub Desktop.
JimTheBot from rhubarb.sgoblin.com
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
import websocket
from time import sleep
import json
# Insert database name here
db_name="jimdb"
# Insert valid database URI here
db_uri="mongodb://jim:jim@example.org/jimdb"
# Insert websocket URI of the rhubarb chat server here
rhubarb_uri="wss://rhubarbserver.example.org/chatsocket"
# Insert acceptable origin for connection to rhubarb chat server here
rhubarb_origin="https://rhubarb.example.org"
name = "JimTheBot"
bot = ChatBot("Terminal",
storage_adapter="chatterbot.storage.MongoDatabaseAdapter",
logic_adapters=[
"chatterbot.logic.ClosestMeaningAdapter",
"chatterbot.logic.ClosestMatchAdapter",
"chatterbot.logic.TimeLogicAdapter",
"chatterbot.logic.MathematicalEvaluation",
{
"import_path": "chatterbot.logic.LowConfidenceAdapter",
"threshold": 0.5,
"default_response": "I'm so, so, sorry! I cannot understand your strange ramblings..."
}
],
database_uri=db_uri,
database=db_name
)
bot.set_trainer(ChatterBotCorpusTrainer)
bot.train("chatterbot.corpus.english")
ws = websocket.create_connection(rhubarb_uri, origin=rhubarb_origin)
try:
ws.send(json.dumps({"name": name, "message": "Hello earthlings!"}))
thingFrom = ws.recv()
while(True):
thingFrom = json.loads(ws.recv())
messageFrom = thingFrom["message"]
whoFrom = thingFrom["name"]
if (whoFrom != name):
print(messageFrom)
messageTo = json.dumps({"name": name, "message": "@" + whoFrom + " " + bot.get_response(messageFrom).text})
ws.send(messageTo)
finally:
ws.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment