Microsoft-Rewards-bot icon indicating copy to clipboard operation
Microsoft-Rewards-bot copied to clipboard

Run bot on arm64 (raspberrypi) automatically every day

Open Guido30 opened this issue 2 years ago • 7 comments

Hello

I have a proposal to implement maybe on another branch the possibility to run this bot as a docker container, specifically on arm64 devices like raspberrypi, the advantages are that it would automatically run everyday and consume a low amount of power since its on a pi

To achieve this i have made some changes to the code, only locally for now, i will explain them here, i've had the bot running for a few days now so im confident this should work

Initially i decided to put this bot in a docker container, the reason is because while implementing these changes i wanted to make sure i didnt clutter my pi os install since it has limited storage (only 8gb), so i made a docker-compose which defines 2 services:

version: "2.0"

services:

  microsoftrewardsbot:
    build: ./bot
    working_dir: /usr/app
    depends_on:
      - selenium
    volumes:
      - "./accounts.json:/usr/app/accounts.json"

  selenium:
    image: seleniarm/standalone-chromium:latest
    shm_size: 2gb
    ports:
    - 4444:4444
    - 5900:5900
    - 7900:7900

The reason i used version 2 is because my pi runs on a custom board which has a custom os based on raspbian, but it hasnt been mantaned, and docker-compose 3 doesnt work, but for most users version 3 should work but might require some changes im not sure, anyways the bot will not use a local chromium driver but instead will connect to the selenium container which runs the driver, in the ms_farmer_rewards.py i have changed ONLY these lines:

1:   #!/usr/bin/env python3
42:  time.sleep(30)
117: browser = webdriver.Remote(command_executor="http://selenium:4444/wd/hub", options=options)
121: # createDisplay()

Im giving 30 seconds to ensure the selenium container is running properly when using docker-compose up, then the browser session will be created using the selenium container, and finally im disabling the createDisplay since the raspberry is running in headless mode and doesnt have a gui

Here's the custom docker image containing the bot'files:

FROM python:3.10-slim-buster

WORKDIR /usr/app
COPY ms_rewards_farmer.py requirements.txt ./

RUN apt-get update && \
    apt-get install -y --no-install-recommends wget iputils-ping python-tk python3-tk tk-dev && \
    rm -rf /var/lib/apt/lists/* && \
    chmod +x ms_rewards_farmer.py && \
    pip install --upgrade pip && \
    pip install -r requirements.txt

ENTRYPOINT ["./ms_rewards_farmer.py"]
CMD ["--headless", "--fast", "--skip-unusual", "--no-webdriver-manager"]

Make sure the files requirements.txt, Dockerfile, ms_farmer_rewards.py are all in the bot folder

The accounts.json file must be in the root folder, since it will be bind mounted into the container when using docker-compose

Now all of this just allows me to run the bot as a containerized app on the raspberry, but i took it a step further and automated its start every day, including the activation of a vpn using protonvpn to farm twice in a day, but if users want to use the vpn, they will have to manually set their closest server, in my case im using netherlands servers.

Anyways to automatically run this i have made a service file, and a timer to start the service once every day, here are the files

rewardsbot.service

[Unit]
Description=Microsoft Rewards Bot Service
After=network.target

[Service]
User=pi
WorkingDirectory=/home/pi/Microsoft-Rewards-Bot-PI
ExecStart=/bin/bash -c 'docker-compose up --force-recreate microsoftrewardsbot && sudo protonvpn c --cc NL && docker-compose up --force-recreate microsoftrewardsbot && sudo protonvpn d'

[Install]
WantedBy=multi-user.target

rewardsbot.timer:

[Unit]
Description=Microsoft Rewards Bot Timer

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Inside the rewardsbot.service file, the path to the project dir must be correct, and the closest server must be changed appropriately (the protonvpn c --cc NL, NL refers to the netherlands server)

The service must be enabled so here are the commands to do so:

sudo cp rewardsbot.service /etc/systemd/system/
sudo cp rewardsbot.timer /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable rewardsbot.service
sudo systemctl enable rewardsbot.timer

Obviously to use the vpn, it needs to be installed and configured, this has to be done on the host os, not in a container, to do so first you have to install protonvpn-cli from here, just follow their install instructions and then use sudo protonvpn init to configure the account credentials, once all of this is done the vpn will be activated when the bot service runs to farm twice in a day

I wouldnt suggest using a country outside of your continent, since i got some accounts banned when i used the japan vpn

Thats it :)

Guido30 avatar Mar 30 '23 00:03 Guido30