This is a rudimentary chatbot for Tinode using gRPC API. It's written in Python as a demonstration that the API is language-independent.
The chat bot subscribes to events stream using Plugin API and logs in as 'Tino the Chatbot' user. The event stream API is used to listen for new accounts. When a new account is created, the bot initiates a p2p topic with the new user. Then it listens for messages sent to the topic and responds to each with a random quote from quotes.txt
file.
Generated files are provided for convenience in a separate folder. You may re-generate them if needed:
python -m grpc_tools.protoc -I../pbx --python_out=. --grpc_python_out=. ../pbx/model.proto
-
Follow instructions to build and run dockerized Tinode chat server up to an including step 4.
-
In the step 5 add the following parameter to the command line:
--volume botdata:/botdata
. It will create and mount a volumebotdata
where Tino's (chatbot's) credentials are stored.docker run --rm --name tinode-init-db --network tinode-net --volume botdata:/botdata tinode-init-db
-
Follow step 6 to build the Tinode server.
-
Run the server adding
--env PLUGIN_PYTHON_CHAT_BOT_ENABLED=true
to the command line:$ docker run -p 6060:18080 -d --name tinode-srv --env PLUGIN_PYTHON_CHAT_BOT_ENABLED=true --network tinode-net tinode-srv
-
If current folder is
chat/chatbot
, then the Dockerfile is located inchat/docker/chatbot
folder;cd ../docker
and build the chat bot image$ docker build --tag=tinode-bot chatbot
-
Run the chatbot
$ docker run -d --name tinode-bot --network tinode-net --volume botdata:/botdata tinode-bot
-
Test that the bot is functional by pointing your browser to http://localhost:6060/x/, login and talk to user
Tino
. The user should respond to every message with a random quote.
Make sure python 2.7 or 3.4 or higher is installed. Make sure pip 9.0.1 or higher is installed. If you are using python 2.7 install futures
:
pip install futures
Follow instructions to install grpc. The package is called grpcio
(not grpc
!):
pip install grpcio
Start the tinode server first. Then start the chatbot with credentials of the user you want to be your bot, alice
in this example:
python chatbot.py --login-basic=alice:alice123
If you want to run the bot in the background, start it as
nohup python chatbot.py --login-basic=alice:alice123 &
Run python chatbot.py -h
for more options.
If you are using python 2.7, keep in mind that condition.wait()
is forever buggy. As a consequence of this bug the bot cannot be terminated with a SIGINT. It has to be stopped with a SIGKILL.
You can use cookie file to store credentials. Sample cookie files are provided as basic-cookie.sample
and token-cookie.sample
. Once authenticated the bot will attempt to store the token in the cookie file, .tn-cookie
by default. If you have a cookie file with the default name, you can run the bot with no parameters:
python chatbot.py
Quotes are read from quotes.txt
by default. The file is plain text with one quote per line.