104 lines
3.3 KiB
Python
104 lines
3.3 KiB
Python
|
import os
|
||
|
import sys
|
||
|
import json
|
||
|
import time
|
||
|
import telepot
|
||
|
import requests
|
||
|
import traceback
|
||
|
from pathlib import Path
|
||
|
import pprint
|
||
|
|
||
|
try:
|
||
|
API_TOKEN = os.environ["TELEGRAM_API_TOKEN"]
|
||
|
except KeyError:
|
||
|
print("TELEGRAM_API_TOKEN enviroment variable is not set.")
|
||
|
exit(1)
|
||
|
|
||
|
CONFIG_FILE = os.environ.get("CONFIG_FILE", "./arti_server_bot.json")
|
||
|
config_file = Path(CONFIG_FILE)
|
||
|
|
||
|
users = {}
|
||
|
package_history = {}
|
||
|
if config_file.exists():
|
||
|
conf = {}
|
||
|
try:
|
||
|
conf = json.loads(config_file.open().read())
|
||
|
except json.decoder.JSONDecodeError as err:
|
||
|
traceback.print_exc()
|
||
|
print("Loading config file failed")
|
||
|
|
||
|
users = conf.get("users", {})
|
||
|
|
||
|
|
||
|
def write_config():
|
||
|
with config_file.open("w") as f:
|
||
|
conf = {"users": users}
|
||
|
f.write(json.dumps(conf, indent=4))
|
||
|
|
||
|
TRACKING_URL = "https://gls-group.eu/app/service/open/rest/EU/en/rstt001?match="
|
||
|
STEAM_CONTROLLER_TRACKING_ID = "52650089530801"
|
||
|
|
||
|
bot = telepot.Bot(API_TOKEN)
|
||
|
|
||
|
|
||
|
def on_message(msg):
|
||
|
content_type, chat_type, chat_id = telepot.glance(msg)
|
||
|
chat_id = str(chat_id)
|
||
|
print(content_type, chat_type, chat_id, msg["from"]["username"], msg["text"])
|
||
|
if content_type == "text" and chat_type == "private":
|
||
|
if msg["text"] == "/start":
|
||
|
if msg["from"]["username"] == "artizirk":
|
||
|
bot.sendMessage(chat_id, "Hello master")
|
||
|
if chat_id not in users:
|
||
|
bot.sendMessage(chat_id, "Saving you")
|
||
|
users[chat_id] = msg["from"]
|
||
|
write_config()
|
||
|
else:
|
||
|
bot.sendMessage(chat_id, "Your chat_id already saved")
|
||
|
else:
|
||
|
bot.sendMessage(chat_id,
|
||
|
"Hello {}".format(msg["from"]["first_name"]))
|
||
|
elif msg["text"] == "/users":
|
||
|
reply = "Current users are:```{}```"
|
||
|
pretty_users = pprint.pformat(users, indent=4)
|
||
|
bot.sendMessage(chat_id, reply.format(pretty_users),
|
||
|
parse_mode="Markdown")
|
||
|
else:
|
||
|
bot.sendMessage(chat_id, "No idea what you just said")
|
||
|
else:
|
||
|
reply = "I'm sorry {}, I'm afraid I cant't do that."
|
||
|
name = msg["from"].get("first_name")
|
||
|
if not name:
|
||
|
name = msg["from"].get("username", "Dave")
|
||
|
bot.sendMessage(chat_id, reply.format(name))
|
||
|
|
||
|
bot.message_loop(on_message)
|
||
|
print("Listening ...")
|
||
|
|
||
|
|
||
|
def parse_delivery_history(resp):
|
||
|
history = resp["tuStatus"][0]["history"]
|
||
|
for event in history:
|
||
|
event["datetime"] = event["date"] + " " + event["time"]
|
||
|
del event["date"]
|
||
|
del event["time"]
|
||
|
yield event
|
||
|
|
||
|
msg_template = "Steam controller status update from:\n`{} ({}) at {}`\n{}"
|
||
|
while True:
|
||
|
resp = requests.get(TRACKING_URL+STEAM_CONTROLLER_TRACKING_ID)
|
||
|
history = list(parse_delivery_history(resp.json()))[::-1]
|
||
|
|
||
|
for event in history:
|
||
|
if event["datetime"] not in package_history:
|
||
|
package_history[event["datetime"]] = event
|
||
|
msg = msg_template.format(
|
||
|
event["address"]["countryName"],
|
||
|
event["address"]["city"],
|
||
|
event["datetime"],
|
||
|
event["evtDscr"]
|
||
|
)
|
||
|
for user in users:
|
||
|
bot.sendMessage(user, msg, parse_mode="Markdown")
|
||
|
time.sleep(60)
|