Bot to help a support service of an online publishing company.
-
Create a DialogFlow
projectwith Google Cloud: -
DialogFlow project has a
Project ID, you'll need it several times. -
Create a DialogFlow
agent(use thatProject IDto connect it to Google Cloud). Don't forget to specify language that users of the bots will write with. -
Agent is like a bot, but from DialogFlow's side of things; your Telegram and VK bots will communicate with this agent.
-
Create service account for your DialogFlow Google Cloud project: https://console.cloud.google.com/iam-admin/serviceaccounts?project={project_id} - where {project_id} is the same
Project IDfrom before. -
That service account will have e-mail. Use it to create a Principal here: https://console.cloud.google.com/iam-admin/iam?project={project_id} Give it two Roles:
Dialogflow Intent AdminandDialogflow Service Agent. -
Then go here: https://console.cloud.google.com/iam-admin/serviceaccounts?project={project_id} , click on e-mail, then on
Keystab and create JSON key-file. Put this file in root directory of the project (after installing it by instructions below).
Python3 should be already installed.
Download the repository:
git clone https://github.com/Katsutami7moto/opc-support-bot.git
cd opc-support-bot
Then use pip (or pip3, if there is a conflict with Python2) to install dependencies:
pip install -r requirements.txt
Then, configure environment variables:
- Go to the project directory and create a file with the name
.env(yes, it has only the extension). This file will contain environment variables that usually store data unique to each user, thus you will need to create your own. - Copy and paste this to
.envfile:
TELEGRAM_BOT_TOKEN='{telegram_token}'
GOOGLE_APPLICATION_CREDENTIALS='{credentials_json_file}'
PROJECT_ID='{project_id}'
LANGUAGE_CODE='{lang}'
VK_CLUB_TOKEN='{vk_club_token}'- Replace
{telegram_token}with API token for the Telegram bot you have created with the help of BotFather. This token will look something like this:958423683:AAEAtJ5Lde5YYfkjergber. - Replace
{credentials_json_file}with the name of JSON key-file you downloaded from the service account settings. - Replace
{project_id}withProject IDof DialogFlow Google Cloud project. - Replace
{lang}with code of your users' language, e.g.ru. - Replace
{vk_club_token}with token of VK club you have created; token is created here: https://vk.com/{club_id}?act=tokens
For Telegram, start chat with the bot you have created. Then run the script with this command:
python3 tg_bot.py
For VK bot, execute this command:
python3 vk_bot.py
To add new phrases for bots to interact with:
- Create a JSON file with name
filename.jsonthat will look like this:
{
"Устройство на работу": {
"questions": [
"Как устроиться к вам на работу?",
"Как устроиться к вам?",
"Как работать у вас?",
"Хочу работать у вас",
"Возможно-ли устроиться к вам?",
"Можно-ли мне поработать у вас?",
"Хочу работать редактором у вас"
],
"answer": "Если вы хотите устроиться к нам, напишите на почту game-of-verbs@gmail.com мини-эссе о себе и прикрепите ваше портфолио."
},
"Забыл пароль": {
"questions": [
"Не помню пароль",
"Не могу войти",
"Проблемы со входом",
"Забыл пароль",
"Забыл логин",
"Восстановить пароль",
"Как восстановить пароль",
"Неправильный логин или пароль",
"Ошибка входа",
"Не могу войти в аккаунт"
],
"answer": "Если вы не можете войти на сайт, воспользуйтесь кнопкой «Забыли пароль?» под формой входа. Вам на почту придёт письмо с дальнейшими инструкциями. Проверьте папку «Спам», иногда письма попадают в неё."
},
...
}- Execute this command:
python3 create_intent.py --path filename.json
By default, this script assumes the name of the file to be questions.json.
- Fork this repository.
- Sign up at Heroku.
- Create an app at Heroku; choose
Europeregion. - In next points,
opc-support-botportion of all links should be changed to the name of Heroku app you have created. - Connect forked GitHub repository.
- Go to Settings and set
Config Varsfrom previously described environment variables, putting each name toKEYand value toVALUE, e.g.TELEGRAM_BOT_TOKENtoKEYand{telegram_token}(here it should be without' 'quotation marks) toVALUE. - Setup Google Application Credentials as described here, but use this buildpack on step 2.
- Go to Deploy section, scroll to bottom, to
Manual Deploy, be sure to choosemainbranch and clickDeploy Branchbutton. - Bot should start working and send you a
Bot is running.message (if you have started the chat with it), but just in case check the logs of the app. At the end it should look something like this:
2022-07-25T12:52:42.000000+00:00 app[api]: Build succeeded
2022-07-25T12:52:42.153483+00:00 heroku[bot.1]: Stopping all processes with SIGTERM
2022-07-25T12:52:42.338522+00:00 heroku[bot.1]: Process exited with status 143
2022-07-25T12:52:42.793206+00:00 heroku[bot.1]: Starting process with command `python3 main.py`
2022-07-25T12:52:43.389877+00:00 heroku[bot.1]: State changed from starting to up
The code is written for educational purposes on online-course for web-developers dvmn.org.