Simple customer review bot
- Introduction - what does the bot do
- /start - to start the bot
- /done - to stop the bot
https://sketchboard.me/ZB6AjLLmvIBv#/
Ensure that pip and python is installed
install pipenv for dependencies management. List of dependencies will be listed on Pipfile. Make sure that pipenv path is added to the system.
pip install --user pipenv
To install dependencies, use Pipfile
pipenv install
There are imcompatible versions in the resolved dependencies:
- requests==2.23.0 (from -r /tmp/pipenvog7ro2t7requirements/pipenv-r7iw7_js-constraints.txt (line 21))
- requests (from cachecontrol==0.12.6->-r /tmp/pipenvog7ro2t7requirements/pipenv-r7iw7_js-constraints.txt (line 33))
- requests<3.0,>=2.21 (from algoliasearch==2.3.0->-r /tmp/pipenvog7ro2t7requirements/pipenv-r7iw7_js-constraints.txt (line 4))
- requests<3.0.0dev,>=2.18.0 (from google-api-core==1.18.0->-r /tmp/pipenvog7ro2t7requirements/pipenv-r7iw7_js-constraints.txt (line 14))
- requests==2.7.0 (from telebot==0.0.3->-r /tmp/pipenvog7ro2t7requirements/pipenv-r7iw7_js-constraints.txt (line 18))
- Create an .env file in the directory
- add python-dotenv lib and os to get access to the variables
Variables description :
- TELE_TOKEN. Token for Telegram Bot
- ALGOLIA_APP_ID. Algolia Application ID for data search
- ALGOLIA_ADMIN_API_KEY. Algolia API key for an application
- ALGOLIA_INDEX_NAME. Algolia index name for keeping/storing data from db
It is a library provides a pure Python interface for Telegram Bot API. First, generate an access token by talking to BotFather by following these steps https://core.telegram.org/bots#6-botfather. Now you are ready to create the bot! There are two most important classes in the bot:
- Updater it continuously fetches all updates from telegram and passes them to Dispatcher
- Dispatcher Updater object will then create a Dispathcer and link them with a Queue. User will then register handlers in the Dispatcher which will sort the updates fetched by the Updater and deliver back a callback function
Handlers that were used :
- CommandHandler
Handler class to handle Telegram commands - MessageHandler
Handler class to handle Telegram messages such as text, media or status update - ConversationHandler
Handler class to hold a conversation with a single user
There are two methods to receive updates from bot:
- Polling (getUpdates) This method works by keep pulling request from telegram server. After adding all the classes with handlers and callback functions, the bot is started by using polling method
- Webhook
Whenever there is an update from the bot, webhook will send HTTP POST request to the specified url, containing JSON format data. In this program, Server model that is used is Webhook with Reverse Proxy and Integrated server. These are the steps to setup webhook with this method:
- Get a public IP (Nginx) with domain name. In this case, a VPS is used. DigitalOcean, Nginx
- Get CA verified SSL certificate with Letsencrypt and CertBot). Refer to this link for the steps
- Setup Reverse proxy with Nginx following this wiki page
- Set Webhook to handle bot with setWbhook and check bot getWebhookInfo to verify if it is done successfully
- Use integrated server in bot that listen to a local address an port following this wiki page
To setup Cloud Firestore follow these steps:
- Create a project in Firestore console
- Navigate to Database section and create a database
- Set up environment by installing the required dependencies and library
- Initialize Cloud Firestore SDK by using Firebase Admin SDK on your server
- go to project settings and navigate to Service accounts tab
- generate new private key (a json file will be donwloaded to your local disk and is ready to use)
- You are ready to add and get data!
Implement firestore into your program:
- set the private key in json file as credential and initialize it
credentials.Certificate('./secrets/cusreview.json') firebase_admin.initialize_app(cred)
- assign a variable as firestore client
db = firestore.client()
- More information of what to do next at Firestore
Database Setup in WIndows can be found here PostgreSQL
Database Setup in linux:
- Create the file /etc/apt/sources.list.d/pgdg.list and add a line for the repository
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
- Import the repository signing key
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
- Update package lists and install packages
sudo apt-get update apt-get install postgresql-12
- Start database server using:
sudo systemctl start postgresql@12-main
- Check database server status
sudo systemctl status postgresql
- Now, you can access into postgresql account. You are able to implement any psql shell command after using:
sudo -u postgres psql
Database setup in Windows can be found here MongoDB
Database setup in Linux:
- Import the public key used by the package management system
if you receive any error indicating gnupg is not installed, install gnupg before importing the private key
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
sudo apt-get install gnupg
- Create list file for Mongodb for Ubuntu (Bionic)
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
- Reload local packages database and install database, database directories will be at /var/lib/mongodb
sudo apt-get update sudo apt-get install -y mongodb-org
- Start Mongodb server with systemctl
sudo systemctl start mongod
- You can verify Mongodb has started successfully
sudo systemctl status mongod
- Now, you can begin using Mongodb using:
mongo
- Spawns a shell within the virtualenv. Run
piepnv shell
- Run this command to start the bot
python bot.py
- Ctrl+C to stop
- Use context.user_data in the handler callback to access to user-specific dict
- Load any value into this dictionary to temporary store information for later use
- Import the class function from files in db folder to bot.py
- After all questions are answered, this dictionary will be used to save into the respective database
- import algoliasearch module
- create variables client to create a search client by validating algolia Admin API and Application ID
- create variable index to browse through an index name
- use Search method to query an index
- return desired data by following the response format in JSON