Giter VIP home page Giter VIP logo

shterneregen / telegram-home-bot Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 538 KB

Telegram bot that provides simple monitoring of hosts in your network and allows you to execute commands on the bot-machine

HTML 18.26% CSS 0.22% JavaScript 3.63% Kotlin 77.73% Dockerfile 0.17%
telegram-bot spring-boot google-charts h2-database spring-security thymeleaf kotlin openweathermap-api webflux home-automation

telegram-home-bot's Introduction

CircleCI

Telegram Home Bot

Can be used for home automation on Raspberry Pi

What can this bot do?

  • Monitor local network and notify about hosts appearances/disappearances via Telegram client
  • Run commands on target machine from Telegram client
  • Wake-up selected hosts via WOL technology sending magic packet using Telegram client
  • Provide weather info for chosen places using OpenWeatherMap API via Telegram client

Application settings are stored in application.properties file

How to set up the project

Create telegram chatbot

  • Start a chat with @BotFather
  • Use message /newbot
  • Set unique bot name (ends with _bot or Bot, use it as telegram.bot-name)
  • Get token from the final message (and use it as telegram.token)
  • Start conversation with bot
  • Retrieve chat_id, call https://api.telegram.org/bot[YOUR_TOKEN]/getUpdates (and use it as telegram.bot-owner-id)

Admin site in browser

  • Located by default on http://127.0.0.1:9988/
    • Port setting: server.port
  • Default users: admin / 1234, user / 1234
    • Default credentials could be changed in settings: default.admin.login / default.admin.password, default.user.login / default.user.password
  • Password could be changed on http://127.0.0.1:9988/updatePassword page

Enable network monitor

  • Set network-monitor.enabled to true
  • You can observe/add/edit hosts on http://127.0.0.1:9988/hosts page
  • Hosts availability changes is on http://127.0.0.1:9988/hosts/time-log page
  • Unfortunately, current implementation is based on ip -j n show and some other native calls, so you have to install iproute2 and fpingon Linux machine where chatbot is running. See Dockerfile
  • This feature does not work on Windows
  • To enable/disable host notifications, run the /features command in the Telegram client and choose what you want

What does network monitor do?

  • Periodically calls a command (state.change.command) to check network changes
  • Chatbot notifies about hosts appearances/disappearances telegram.bot-owner-id user

Run commands on chatbot machine

  • You can add any initial commands that will be available to run in commands.properties file
  • Besides, you can add/edit commands on http://127.0.0.1:9988/commands page
  • To hide any command from the Telegram client, uncheck the Enabled checkbox on the Command editing page
  • In the Telegram client these commands will be available as a reply markup

Wake On Lan feature

  • Modify wakeOnLan.broadcast.ip according to your network mask
  • To enable WOL for a host open http://127.0.0.1:9988/hosts page
  • Choose a host and check Wake On Lan Enabled checkbox
  • To wake up a host, run /wol in the Telegram client and select the one you want

Enable Weather menu

List of chatbot commands

  • /menu - Main menu
  • /features - Features Setting (notification settings for now)
  • /wol - Wake On Lan hosts
  • /weather - Weather

Additional information

  • Run gradlew clean bootJar in the project root folder

  • Executable jar will be placed here: build/libs/thb.jar

  • Copy the jar file to SOME_LINUX_FOLDER on Linux machine

  • Run on Linux machine:

sudo mkdir /var/telegram # create a folder for jar file
sudo cp /SOME_LINUX_FOLDER/thb.jar /var/telegram/thb.jar # copy jar to the folder
sudo ln -s /var/telegram/thb.jar /etc/init.d/thb # create symlink the jar to init.d
sudo chmod +x /var/telegram/thb.jar # make thb.jar executable
sudo systemctl daemon-reload # reload systemd manager configuration
sudo service thb start # start bot as a service
update-rc.d thb defaults # autostart
  • When I'm using THB as a Linux service, I just put the properties files next to the jar file and also set the Telegram credentials into the application.properties without using env variables

Enable HTTPS

The example below will be with a self-signed certificate on the local machine

  • Create thb-keystore.p12
keytool -genkeypair -alias thb -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore thb-keystore.p12 -validity 3650 -ext san=ip:127.0.0.1
  • You will be asked to enter a password for the keystore
  • Uncomment server.ssl.* properties
  • Set your password as server.ssl.key-store-password property and change other props if necessary
  • Add thb-keystore.p12 to the Trusted Root Certification Authorities certificate store (Windows)

Launch bot in Docker

# Build the image
docker build -t thb-image .

# Create and start new container from the image
docker run -d -p 80:8080 --network=bridge --name=thb  thb-image

# Start the container
docker start thb

# Stop the running container
docker stop thb

# Show information logged by a running container
docker logs -f thb

Launch SonarQube in Docker

  • Run SonarQube container
docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube
  • Browse http://127.0.0.1:9000. Initial credentials: admin / admin
  • Change password
  • Create project with name thb
  • Create project token
  • Run sonarqube task
gradlew sonarqube -Dsonar.projectKey=thb -Dsonar.host.url=http://127.0.0.1:9000 -Dsonar.login=PROJECT_TOKEN

Create private chat group with chatbot (JFYI)


Preview (outdated)

Commands

Hosts

TimeLog


Kotlin, Spring (Boot/Security/WebFlux), Gradle, H2, Thymeleaf, Google Charts, telegrambots-spring-boot-starter

telegram-home-bot's People

Contributors

shterneregen avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

telegram-home-bot's Issues

published on dockerhub

Hi there,

thank you very much for this awesome project!

I'm using it on multiple devices, which is why i've published it on dockerhub (in order to pull the image on my devices). I hope you are fine with that. Unfortunately, i cant't make the image private because I don't have a premium account on dockerhub..

I've made a link to this repo though. Please check: https://hub.docker.com/r/mualm/telegram-home-bot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.