mirror of
https://codeberg.org/mclemens/pyBMNotify.git
synced 2024-12-22 21:46:28 -05:00
code cleanup
This commit is contained in:
parent
047e630252
commit
3bd119e899
@ -6,6 +6,7 @@ import time
|
|||||||
import config as cfg
|
import config as cfg
|
||||||
import http.client, urllib
|
import http.client, urllib
|
||||||
|
|
||||||
|
# libraries only needed if Telegram is configured in config.py
|
||||||
if cfg.telegram_api_id != "" and cfg.telegram_api_hash != "" and cfg.telegram_username != "":
|
if cfg.telegram_api_id != "" and cfg.telegram_api_hash != "" and cfg.telegram_username != "":
|
||||||
import telebot
|
import telebot
|
||||||
from telethon.sync import TelegramClient
|
from telethon.sync import TelegramClient
|
||||||
@ -24,7 +25,9 @@ def on_disconnect():
|
|||||||
def on_reconnect():
|
def on_reconnect():
|
||||||
print('Reconnecting')
|
print('Reconnecting')
|
||||||
|
|
||||||
|
# Send push notification
|
||||||
def push_message(msg):
|
def push_message(msg):
|
||||||
|
# Push notification via Pushover. Disabled if not configured in config.py
|
||||||
if cfg.pushover_token != "" and cfg.pushover_user != "":
|
if cfg.pushover_token != "" and cfg.pushover_user != "":
|
||||||
conn = http.client.HTTPSConnection("api.pushover.net:443")
|
conn = http.client.HTTPSConnection("api.pushover.net:443")
|
||||||
conn.request("POST", "/1/messages.json",
|
conn.request("POST", "/1/messages.json",
|
||||||
@ -34,25 +37,21 @@ def push_message(msg):
|
|||||||
"message": msg,
|
"message": msg,
|
||||||
}), { "Content-type": "application/x-www-form-urlencoded" })
|
}), { "Content-type": "application/x-www-form-urlencoded" })
|
||||||
conn.getresponse()
|
conn.getresponse()
|
||||||
|
# Push notification via Telegram. Disabled if not configured in config.py
|
||||||
if cfg.telegram_api_id != "" and cfg.telegram_api_hash != "" and cfg.telegram_username != "" and cfg.phone != "":
|
if cfg.telegram_api_id != "" and cfg.telegram_api_hash != "" and cfg.telegram_username != "" and cfg.phone != "":
|
||||||
# creating a telegram session and assigning it to a variable client
|
|
||||||
client = TelegramClient('bm_bot', cfg.telegram_api_id, cfg.telegram_api_hash)
|
client = TelegramClient('bm_bot', cfg.telegram_api_id, cfg.telegram_api_hash)
|
||||||
# connecting and building the session
|
|
||||||
client.connect()
|
client.connect()
|
||||||
# in case of script ran first time it will ask either to input token or otp sent to
|
|
||||||
# number or sent or your telegram id
|
|
||||||
if not client.is_user_authorized():
|
if not client.is_user_authorized():
|
||||||
client.send_code_request(cfg.phone)
|
client.send_code_request(cfg.phone)
|
||||||
# signing in the client
|
client.sign_in(cfg.phone, input('Please enter the code which has been sent to your phone: '))
|
||||||
client.sign_in(cfg.phone, input('Enter the code: '))
|
|
||||||
try:
|
try:
|
||||||
receiver = InputPeerUser('user_id', 'user_hash')
|
receiver = InputPeerUser('user_id', 'user_hash')
|
||||||
client.send_message(cfg.telegram_username, msg)
|
client.send_message(cfg.telegram_username, msg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e);
|
print(e);
|
||||||
# disconnecting the telegram session
|
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
|
||||||
|
# assemble the text message
|
||||||
def construct_message(c):
|
def construct_message(c):
|
||||||
tg = c["DestinationID"]
|
tg = c["DestinationID"]
|
||||||
out = ""
|
out = ""
|
||||||
@ -66,6 +65,7 @@ def construct_message(c):
|
|||||||
# finally return the text message
|
# finally return the text message
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
# where the magic happens
|
||||||
def on_mqtt(*args):
|
def on_mqtt(*args):
|
||||||
# get json data of transmission
|
# get json data of transmission
|
||||||
call = json.loads(args[0]['payload'])
|
call = json.loads(args[0]['payload'])
|
||||||
|
Loading…
Reference in New Issue
Block a user