Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
cxo05 committed May 3, 2021
1 parent 5e57df3 commit 69f4383
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
34 changes: 14 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,38 @@ Requirements :
git clone https://github.com/2x0c0h1/BIBO_Bot.git
cd BIBO_Bot
#Create .env variables
echo bot_key=_INSERTKEYHERE_ >> .env
echo databasePath=bibo.db >> .env
echo dev_chat_id=_ID_ >> .env
cat > .env << EOF
bot_key=_INSERTKEYHERE_
databasePath=bibo.db
EOF
#Install packages
pip install -r requirements.txt
#Running the bot
py biboBot.py
```

#Setting up raspberry pi for deployment
#Deployment to raspberry pi

Learning materials:

* [Raspberrypi Networking](https://raspberrypi.stackexchange.com/questions/37920/how-do-i-set-up-networking-wifi-static-ip-address-on-raspbian-raspberry-pi-os/37921#37921)
* [Systemd](https://github.com/torfsen/python-systemd-tutorial)

```bash
#SSH
#Connect to pi assuming its already setup
ssh pi@IP_ADDRESS
#Change network if necessary
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
cd ~
#Same as development
git clone https://github.com/2x0c0h1/BIBO_Bot.git
cd BIBO_Bot
#Create .env variables
echo bot_key=_INSERTKEYHERE_ >> .env
echo databasePath=bibo.db >> .env
echo dev_chat_id=_ID_ >> .env
#Install packages
pip install -r requirements.txt
#Copy bibo.service to /etc/systemd/user
cp bibo.service /etc/systemd/user
...
...
#Copy bibo.service to /etc/systemd/system
cp bibo.service /etc/systemd/system
#Reload
systemctl --user daemon-reload
systemctl daemon-reload
#Enable autorun on boot
systemctl --user enable bibo
sudo loginctl enable-linger $USER
#Check if service is enabled
systemctl --user list-unit-files
systemctl enable bibo
#Check/debug service
systemctl status bibo
```
14 changes: 10 additions & 4 deletions bibo.service
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
[Unit]
Description=BIBO Telegram Bot Service
Description=BIBO Service
After=network-online.target
Wants=network-online.target
StartLimitIntervalSec=0

[Service]
ExecStart=/usr/bin/python ~/BIBO_Bot/biboBot.py
Restart=on-failure
User=pi
Restart=always
RestartSec=60
Type=simple
ExecStart=/usr/bin/python3 /home/pi/BIBO_Bot/biboBot.py

[Install]
WantedBy=default.target
WantedBy=multi-user.target
10 changes: 1 addition & 9 deletions biboBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
botKey = os.environ.get("bot_key") #Create a .env file in root folder with bot_key=INSERTKEYHERE
databasePath = os.environ.get("databasePath") #Create new line with databasePath=INSERTDATABASEPATHHERE
DEVELOPER_CHAT_ID = os.environ.get("dev_chat_id") #Create new line with dev_chat_id=INSERTCHATIDHERE
import sqlite3
import logging
from sqlite3 import Error
Expand All @@ -16,13 +15,11 @@
MessageHandler,
Filters
)
from telegram.error import TimedOut, NetworkError, TelegramError
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove
from math import sin, cos, sqrt, atan2, radians
from datetime import datetime
import telegramcalendar


logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -168,14 +165,13 @@ def authenticateLocation(update, context):

distance = R * c

#Changing radius from a point in camp to register too far or near
#Checking distance
if distance>1:
update.message.reply_text("Too far from camp, move closer and resend your location")
return LOCATION
else:
conn = connect_database(databasePath)
update.message.reply_text("Location valid")
#status is a bool (0 = Book in, 1 = Book out)
sql = 'INSERT INTO timesheet (telegram_id, time_in) VALUES (?,?)'
args = (update.message.chat_id, datetime.now())
execute_sql(conn, sql, args)
Expand All @@ -184,7 +180,6 @@ def authenticateLocation(update, context):
def testbookIn(update, context):
conn = connect_database(databasePath)
update.message.reply_text("Location valid")
#status is a bool (0 = Book in, 1 = Book out)
sql = 'INSERT INTO timesheet (telegram_id, time_in) VALUES (?,?)'
args = (update.message.chat_id, datetime.now())
execute_sql(conn, sql, args)
Expand Down Expand Up @@ -305,9 +300,6 @@ def cancel(update, context):

def error_handler(update, context):
logger.warning('Update "%s" caused error "%s"', update, context.error)
updater.stop()
updater.is_idle = False
#Definitely not the best solution but the fastest one to get systemctl to restart the service

if __name__=="__main__":
sql_create_company_table = """CREATE TABLE IF NOT EXISTS company (
Expand Down

0 comments on commit 69f4383

Please sign in to comment.