Skip to content

Telegram bot based on Flask, python-telegram-bot, Moltin, Redis

License

Notifications You must be signed in to change notification settings

olegush/pizza-bot

Repository files navigation

Pizza Bot

This telegram bot is simple e-shop based on Moltin API and uses state machine principles. Moltin CSM stores products, user cart and info. Redis DB stores current user statement.

How to install and deploy

  1. Get domain and SSL-certificate, put url to .env file.
URL=https://example.com
  1. Python 3 and libraries from requirements.txt should be installed. Use virtual environment tool, for example venv
python3 -m venv venv_folder_name
source venv_folder_name/bin/activate
python3 -m pip install -r requirements.txt
  1. Use WSGI - server, for example Gunicorn. Create service:
$ sudo nano /etc/systemd/system/app.service

[Unit]
Description=gunicorn daemon
Requires=app.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=path_to_project
Environment="PATH=path_to_virtual_env_bin"
ExecStart=path_to_gunicorn \
          --access-logfile - \
          --workers 3 \
          --timeout 3000 \
          --bind unix:/run/app.sock \
          wsgi:app

[Install]
WantedBy=multi-user.target
  1. Add Nginx config file:
$ sudo nano /etc/nginx/sites-available/project_name
server {
    server_name    example.com;
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/app.sock;
    }
}
  1. Create Moltin account and get Moltin Client ID and Client secret. Put these parameters to .env file. Add your currency https://dashboard.moltin.com/app/settings/currencies
MOLTIN_CLIENT_ID=moltin_client_id
MOLTIN_CLIENT_SECRET=moltin_client_secret
  1. For menu export you should have json file like this:
[{
	"id": id,
	"name": name,
	"description": description,
	"food_value": {
		"fats": fats,
		"proteins": proteins,
		"carbohydrates": carbohydrates,
		"kiloCalories": kiloCalories,
		"weight": weight
	},
	"culture_name": "ru-RU",
	"product_image": {
		"url": img_url,
		"height": img_height,
		"width": img_width
	},
	"price": price
},
]

save this file on the URL_MENU and add .env with the parameter

URL_MENU=url_with_menu_json

Set the width of small image with WIDTH_SMALL constant and run export_menu.py. Then the process is finished, check your products here. Also you'll got original and resized images in the IMAGES_DIR folder.

  1. For addresses export you should have json file like this:
[{
	"id": id,
	"alias": address_alias,
	"address": {
		"full": full_address,
		"city": city,
		"street": street,
		"street_type": street_type,
		"building": building
	},
	"coordinates": {
		"lat": latitude,
		"lon": longitude
	}
},
]

save this file on the URL_ADDRESSES and add it in the .env

URL_ADDRESSES=url_with_addresses_json

Set MOLTIN_FLOW_ADDRESSES constant in the common.py. This is the name of your flow with addresses. Then set FLOW_FIELDS constant in the export_addresses.py, MOLTIN_FLOW_ADDRESSES_ID ennviroment variable in the .env, run the file export_addresses.py and check FLOWS section on your dashboard

  1. To create customers flow set MOLTIN_FLOW_CUSTOMERS constant in the common.py. This is the name of your flow with customers. Then set FLOW_FIELDS constant in the create_customers.py, MOLTIN_FLOW_CUSTOMERS_ID ennviroment variable in the .env, run the file create_customers.py and check FLOWS section on your dashboard. Aslo, common.py contains more useful functions for work with Moltin API and you can use them.

  2. Create new Telegram bot, get token and your ID.

  3. Create Redis account, get host, port and password.

  4. Update .env file.

TG=telegram_token
TELEGRAM_CHAT_ID_ADMIN=telegram_chat_id_admin
REDIS_HOST=redis_host
REDIS_PORT=redis_port
REDIS_PWD=redis_pwd
PAYMENT_PAYLOAD=your_secret_payment_payload
PAYMENT_TOKEN_TRANZZO=payment_token

Quickstart

Run main.py and test your e-shop in Telegram.

pizza-shop screenshot

Project Goals

The code is written for educational purposes on online-course for web-developers dvmn.org.

About

Telegram bot based on Flask, python-telegram-bot, Moltin, Redis

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages