Skip to content

Commit

Permalink
Merge pull request #3 from JakobLichterfeld/feature/2-python-implemen…
Browse files Browse the repository at this point in the history
…tation

Python implementation, closes: #2
  • Loading branch information
JakobLichterfeld authored Sep 4, 2020
2 parents 6b7945d + 1bdbf09 commit f06b58b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Enhancements

- Implemetation in python, as there are libraries for mqtt and telegram bot available for cool features in the future

### Bug Fixes

## [0.1.0] - 2020-09-04
Expand Down
12 changes: 12 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Lines starting with '#' are comments.
# Each line is a file pattern followed by one or more owners.

# These owners will be the default owners for everything in the repo.


# Order is important. The last matching pattern has the most precedence.
# So if a pull request only touches javascript files, only these owners
# will be requested to review.


# You can also use email addresses if you prefer.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ A telegram bot which sends a message if an update for your Tesla is available (k

## Installation

Install [Dependencies](#dependencies) and fullfill the [Requirements](#requirements).
Install [Dependencies](#dependencies) and fulfill the [Requirements](#requirements).

It is recommended to backup your data first.

Expand All @@ -50,18 +50,19 @@ 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/check_for_tesla_update_and_send_telegram.sh``` and adopt the following lines with your own config:
Open the file ```src/teslamte_telegram_bot.py``` and adopt the following lines with your own config:
```
mqtt_broker=xxx.xxx.xxx.xxx
bot = Bot('api_key')
```
and
```
telegram_bot_token=SECRET:MORE_SECRET
telegram_chat_id=SECRET_CHAT_ID
chat_id='chat_id',
```
```
client.connect(mqtt_config.mqtt_data['mqtt_broker_host', 'mqtt_broker_port', 60)
```

## Usage
Execute the file ```src/check_for_tesla_update_and_send_telegram.sh``` from linux bash with ```sh ./src/check_for_tesla_update_and_send_telegram.sh```
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```

## Update

Expand Down
19 changes: 0 additions & 19 deletions src/check_for_tesla_update_and_send_telegram.sh

This file was deleted.

56 changes: 56 additions & 0 deletions src/teslamte_telegram_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import paho.mqtt.client as mqtt

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

# initializing the bot with API
bot = Bot('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")

# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.

#client.subscribe("teslamate/cars/1/version")
client.subscribe("teslamate/cars/1/update_available")


# The callback for when a PUBLISH message is received from the server.


def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload.decode()))

if msg.payload.decode() == "true":
print("A new SW update for your Tesla is available!")
bot.send_message(
chat_id='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,
)


client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

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


# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
# client.loop_forever()


client.loop_start() # start the loop

client.disconnect()

client.loop_stop()

0 comments on commit f06b58b

Please sign in to comment.