diff --git a/CHANGELOG.md b/CHANGELOG.md index 3eeba73..e99ba34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 \ No newline at end of file +[0.1.0]: https://github.com/JakobLichterfeld/TeslaMate_Telegram_Bot/compare/e76bb37d3...v0.1.0 diff --git a/README.md b/README.md index e9ac577..1376b66 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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! diff --git a/src/mqtt_config.py b/src/mqtt_config.py new file mode 100644 index 0000000..a7a928d --- /dev/null +++ b/src/mqtt_config.py @@ -0,0 +1,4 @@ +mqtt_data = { + 'mqtt_broker_host': 'xxx.xxx.xxx.xxx', + 'mqtt_broker_port': '1883' +} diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000..40568b8 --- /dev/null +++ b/src/requirements.txt @@ -0,0 +1,2 @@ +paho-mqtt==1.5.0 +python-telegram-bot==12.8 diff --git a/src/telegram_config.py b/src/telegram_config.py new file mode 100644 index 0000000..b01adcf --- /dev/null +++ b/src/telegram_config.py @@ -0,0 +1,4 @@ +telegram_bot_data = { + 'api_key': 'SECRET_API_KEY', + 'chat_id': 'CHAT_ID' +} diff --git a/src/teslamte_telegram_bot.py b/src/teslamte_telegram_bot.py index d2cde5c..3777afa 100644 --- a/src/teslamte_telegram_bot.py +++ b/src/teslamte_telegram_bot.py @@ -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. @@ -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=""+"SW Update"+"\n"+"A new SW update for your Tesla is available!\n\n"+msg.topic+"\n"+str(msg.payload.decode()), text=""+"SW Update"+"\n"+"A new SW update for your Tesla is available!", parse_mode=ParseMode.HTML, @@ -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 @@ -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()