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.
- Get domain and SSL-certificate, put url to .env file.
URL=https://example.com
- 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
- 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
- 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;
}
}
- 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
- 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.
- 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
-
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.
-
Create new Telegram bot, get token and your ID.
-
Create Redis account, get host, port and password.
-
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
Run main.py and test your e-shop in Telegram.
The code is written for educational purposes on online-course for web-developers dvmn.org.