Skip to content

Commit

Permalink
config-with-files:
Browse files Browse the repository at this point in the history
Enhancements
- store configuration in seperate files, see #4
- possibility to exit with STRG+C
- bit better messages on connection
  • Loading branch information
JakobLichterfeld committed Sep 4, 2020
1 parent 351300f commit 65e340a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

### Enhancements

- store configuration in seperate files, see #4
- possibility to exit with STRG+C
- bit better messages on connection

### Bug Fixes

## [0.2.0] - 2020-09-04
Expand All @@ -24,4 +28,4 @@

[unreleased]: https://github.com/JakobLichterfeld/TeslaMate_Telegram_Bot/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/JakobLichterfeld/TeslaMate_Telegram_Bot/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/JakobLichterfeld/TeslaMate_Telegram_Bot/compare/e76bb37d3...v0.1.0
[0.1.0]: https://github.com/JakobLichterfeld/TeslaMate_Telegram_Bot/compare/e76bb37d3...v0.1.0
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# TeslaMate Telegram Bot

A telegram bot which sends a message if an update for your Tesla is available (use TeslaMate MQTT)

[![latest release](https://img.shields.io/github/v/release/JakobLichterfeld/TeslaMate_Telegram_Bot)](https://github.com/JakobLichterfeld/TeslaMate_Telegram_Bot/releases/latest)
Expand Down Expand Up @@ -50,25 +51,22 @@ This setup is recommended only if you are running TeslaMate Telegram Bot **on yo

Before the first install you need to set up a telegram bot. For doing so, see [Requirements](#requirements).

Open the file ```src/teslamte_telegram_bot.py``` and adopt the following lines with your own config:
```
bot = Bot('api_key')
```
```
chat_id='chat_id',
```
```
client.connect(mqtt_config.mqtt_data['mqtt_broker_host', 'mqtt_broker_port', 60)
Open the files ```src/mqtt_config.py``` and ```src/telegram_config.py``` and adopt according to your own config.

You also need to install the required pip packages

```shell
python -m pip install -r ./src/requirements.txt
```

## Usage
Execute the file ```src/teslamte_telegram_bot.py.sh``` from linux bash with ```python ./src/check_for_tesla_update_and_send_telegram.sh``` or, depending on your system ```python3 ./src/check_for_tesla_update_and_send_telegram.sh```

Execute the file ```src/teslamte_telegram_bot.py``` from linux bash with ```python ./src/teslamte_telegram_bot.py``` or, depending on your system ```python3 ./src/teslamte_telegram_bot.py```

## Update

Check out the [release notes](https://github.com/JakobLichterfeld/TeslaMate_Telegram_Bot/releases) before upgrading!


## Contributing

All contributions are welcome and greatly appreciated!
Expand Down
4 changes: 4 additions & 0 deletions src/mqtt_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mqtt_data = {
'mqtt_broker_host': 'xxx.xxx.xxx.xxx',
'mqtt_broker_port': '1883'
}
2 changes: 2 additions & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
paho-mqtt==1.5.0
python-telegram-bot==12.8
4 changes: 4 additions & 0 deletions src/telegram_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
telegram_bot_data = {
'api_key': 'SECRET_API_KEY',
'chat_id': 'CHAT_ID'
}
29 changes: 25 additions & 4 deletions src/teslamte_telegram_bot.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
# import config data
import telegram_config
import mqtt_config

import paho.mqtt.client as mqtt

import time

from telegram.bot import Bot
from telegram.parsemode import ParseMode

# initializing the bot with API
bot = Bot('api_key')
bot = Bot(telegram_config.telegram_bot_data['api_key'])

# The callback for when the client receives a CONNACK response from the server.


def on_connect(client, userdata, flags, rc):
print("Connected successfully to broker")
print("Connected with result code "+str(rc))
if rc == 0:
print("Connected successfully to broker")
else:
print("Connection failed")

# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
Expand All @@ -28,7 +38,7 @@ def on_message(client, userdata, msg):
if msg.payload.decode() == "true":
print("A new SW update for your Tesla is available!")
bot.send_message(
chat_id='chat_id',
chat_id=telegram_config.telegram_bot_data['chat_id'],
# text="<b>"+"SW Update"+"</b>\n"+"A new SW update for your Tesla is available!\n\n<b>"+msg.topic+"</b>\n"+str(msg.payload.decode()),
text="<b>"+"SW Update"+"</b>\n"+"A new SW update for your Tesla is available!",
parse_mode=ParseMode.HTML,
Expand All @@ -39,7 +49,8 @@ def on_message(client, userdata, msg):
client.on_connect = on_connect
client.on_message = on_message

client.connect(mqtt_config.mqtt_data['mqtt_broker_host', 'mqtt_broker_port', 60)
client.connect(mqtt_config.mqtt_data['mqtt_broker_host'], int(
mqtt_config.mqtt_data['mqtt_broker_port']), 60)


# Blocking call that processes network traffic, dispatches callbacks and
Expand All @@ -50,6 +61,16 @@ def on_message(client, userdata, msg):


client.loop_start() # start the loop
try:

while True:

time.sleep(1)

except KeyboardInterrupt:

print("exiting")


client.disconnect()

Expand Down

0 comments on commit 65e340a

Please sign in to comment.