Skip to content
This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
mike7seven edited this page Nov 5, 2021 · 47 revisions

presence-bot

Presence is a slack bot that tells you who is in the office or at home. Park it on a Raspberry Pi, hide it in a cupboard.

Setup

Step 1: Install the necessary software first (Debian Based Linux Distro):

1.A (Update distribution to latest version) sudo apt-get update 
1.B (Upgrade distribution to latest version) sudo apt-get upgrade 
1.C (Install arp-scan) sudo apt-get install arp-scan 
1.D (Install mysql server) sudo apt-get install mysql-server
1.E (Install software-properties-common Caution: Adds management for additional software sources) sudo apt -y install software-properties-common 
1.F (Add additional software repository Caution: Adds management for additional software sources) sudo add-apt-repository ppa:ondrej/php
1.G (Additional Update distribution to latest version) sudo apt-get update 
1.H (Additional Upgrade distribution to latest version) sudo apt-get upgrade
1.I (Install php7.0) sudo apt -y install php7.0
1.J (Check the php version installed. The output should be PHP7.0.xx where xx is the latest patch version number) php -v
1.K (Install the mysql module for php7) sudo apt-get install php7.0-mysql 
1.L (Install curl, if not already installed) sudo apt-get install curl 
1.M (Install the curl module for php7) sudo apt-get install php7.0-curl
1.N (Install the libapache2-mod-php7.0 module for php7. This may already be installed) sudo apt-get install libapache2-mod-php7.0
1.O (Install the php7.0-opcache module. This may already be installed) sudo apt-get install php7.0-opcache 

Step 2: Confirm if the MySQL server is running

2.A Enter the command: sudo service mysql status. If you receive the message  * MySQL is stopped. Then move on to step 2.A.2
2.B (Start the MySQL server) sudo service mysql start. 
2.C (Confirm if the MySQL server is running) sudo service mysql status
  Output should look something similar to this if this MySQL server is up and running:  * /usr/bin/mysqladmin  Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu)) Copyright (c) 2000, 2021, Oracle and/or its affiliates. 

Step 3: Create a MySQL database and root database user

3.A Enter the command: sudo mysql -uroot -p
  * When prompted enter your sudo password
  * When prompted enter a password for root user of the database. **Please remember to store the database password**
  * You are now in the mysql command line.
3.B (Create the database.)  Enter the command: create database presence;
3.C Exit the mysql command line. Enter the command: exit

Step 4: Clone repository locally

git clone https://github.com/Denhac/presence-bot
(You may need to install git first using the command "sudo apt-get install git")

Step 5: Install mbstring and composer

Install mbstring first

sudo apt-get install php7.0-mbstring

curl -sS https://getcomposer.org/installer | php

sudo mv composer.phar /usr/local/bin/composer

Run composer in the presence-bot directory

composer install

Composer update after installation

composer update

To verify if composer installed and setup correctly run the *ls* command


 root@raspberrypi:/home/pi/presence-bot# 
 composer.json  composer.lock  composer.phar  LICENSE  presence	README.md  src	vendor

Step 6: Create a .env file. Make sure you have a valid token from here https://www.slack.com/apps/A0F7YS25R-bots

      cd presence-bot/

      sudo nano .env

      DB_HOST=localhost
      DB_NAME=presence
      DB_USERNAME=
      DB_PASSWORD=
      BOT_TOKEN=
      SUPPORT_EMAIL=

Step 7: Cron Task Setup

Set up a minutely cron task under root that runs php presence scan.

sudo crontab -e

Then add in this line at the end.

     */1 * * * * /usr/bin/php /home/pi/presence-bot/presence scan

Step 8: Starting the database and presence bot

The command "php presence bot" will start up the bot, it needs root permission to do the arp scan, so either sudo or use root.

Make sure you're in the presence-bot directory and run the following commands

sudo php presence database << This will start the database
sudo php presence bot << This will start the bot

Now verify the bot works via @presence help in any Slack channel or directly with the bot.

Step 9: Create a systemd file so that the bot will start and run when the operating starts.

 [Unit]
 Description=Presence Service
 [Service]
 ExecStart=/usr/bin/php /home/pi/presence-bot/presence bot
 Restart=always
 RestartSec=3
 StandardOutput=syslog
 StandardError=syslog
 SyslogIdentifier=Presence Service
 User=root
 Group=root
 [Install]
 WantedBy=multi-user.target

Step 10: Enabling the bot to run at startup as a service.

Commands to enable the service

 $ sudo systemctl enable  
 $ sudo systemctl start 

Other helpful systemd commands $ sudo systemctl stop

 $ sudo systemctl disable  

 $ sudo systemctl daemon-reload 

bot commands

the bot is triggered via presence or @{thbotsusername}

  • register xx:xx:xx:xx:xx:xx to associate yourself with a mac address
  • remove xx:xx:xx:xx:xx:xx to undo previous association
  • who is here, whoishere, who's here I'll let you know who is in the house
  • top x listing of the most active users 1-10
  • whoami tells you what devices are registered to you.

possible things to do

  • A command that tells you when a user was there the last time (e.g: mawk was seen 3 days ago)
  • Bot can see a user connected that wasn't there earlier and send an announcement (mawk arrived!)
  • Assign labels to each device of yours.
  • Register devices so they are ignored (other pis, printers etc...)
  • Suggestions? create an issue!