Telegram bot for our BMW G series part 3

In the final part 3, I will try to briefly explain how to launch a telegram bot on a VPS. Previous parts are available here and here.

Register on any VPS hosting.

You can even run a VPS for free on Amazon AWS, but the free server does not work for long and you need a VISA or MASTERCARD bank card to register. So in my case this is not an option.

I used HOSTKEY, but you can use absolutely any hosting. After registration, click New server and select vm.pico for 300 rubles per month and click Order.

Select the preinstalled OS Ubuntu 22.04 and click Order.

Next, you will need to pay and the server installation will begin.

After installation, go to the VPS using the command line (CMD) and enter

 ssh root@1XX.1XX.XX.XX

Enter the password and finally everything is ready to install packages.

Now we need to install the packages we need.

Python is usually already pre-installed. Let's install PIP, SQLITE, MC, SUPERVISOR and CRON.

sudo apt install python3-pip 
sudo apt install sqlite3 
sudo apt install mc
sudo apt install supervisor
sudo apt install cron

We will need PIP to install packages for python, SQLITE to save data downloaded from the car, MC (Midnight Commander) can be used for ease of working with files. We will use SUPERVISOR and CRON to launch the Bot and periodically upload data from Auto.

The program for polling BMW and recording data in SQLite was described in the first part; for it to work, you need to install the following components on the VPS.

sudo pip install asyncio 
sudo pip install bimmer_connected

The Telegram Bot program was described in the second part of the story; for it to work, you will need to install the following components on the VPS.

sudo pip install logging
sudo pip install telebot
sudo pip install http.client
sudo pip install schedule
sudo pip install time
sudo pip install math

We will save the code from the two previous parts of the story into two files BMW.py and BOT.py, respectively. Now you need to copy the created files to the VPS. I do this through FileZilla. You can use any other SFTP file browser to download.

We enter the server’s IP address, login and password in FileZilla and connect to our server. Next, copy our created files to the /home/ folder, log into the server via SSH and check that the files have been copied.

Let's make the downloaded files executable using the chmod command

sudo chmod +x /home/BMW.py
sudo chmod +x /home/BOT.py

The functionality of copied programs can be checked using:

sudo python3 /home/BMW.py
sudo python3 /home/BOT.py

In my case, an error occurred about the absence of the database file /home/mydatabase.db and in the BMW.py and BOT.py files it was necessary to specify the address to the Database in detail – /home/mydatabase.db . That's it, after that everything started without problems.

Now all that's left to do is set up Supervisor and cron.

Open the cron configuration to add the script.

sudo crontab -e

The first time the system will ask you to select an editor. Press 1, Nano will suit us.

Add the following line:

*/30 * * * * python3 /home/BMW.py >> /home/BMW.log 2>&1

Press CTRL+O to save and CTRL+X to exit.

The above script means that BMW.py will run once every 30 minutes. I didn’t dare interrogate the data more often.

BOT.py should run constantly and if something happens to the process, the system should restart the process. For such a task, it is more convenient to use supervisor instead of cron.

To do this, launch Midnigth Commander using the mc command and go to the /etc/supervisor/conf.d/ folder

Using Midnitgh Commander you will need to create a BMW.conf file with the following content

[program:BMW]
command=python3 /home/BMW.py
autorestart=true
startssecs=300 ; 5 minutes (60 seconds * 5 minutes)
stderr_logfile=/home/BMW_err.log
stdout_logfile=/home/BMW_out.log

Next, exit MC and run the commands:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start all

To this day, my bot works without problems. It’s even surprising that there are no failures from the BMW servers while requests come without problems, pah pah pah… Sometimes I think about adding new features and making it public. But for publicity, you need to Encrypt the login and password from the Connected drive, and this is a more serious approach to the matter… I would be glad if you write thoughts of interesting ideas on what you can come up with with car data…

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *