From 0a98f8edaaf2d5887624a499e19076c1061b0ed8 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 13 Aug 2016 14:19:41 +0200 Subject: [PATCH] Send messages to telegram --- .gitignore | 1 + main.py | 7 +++++++ telegram.py | 23 +++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 telegram.py diff --git a/.gitignore b/.gitignore index 430ccab2..099d0e4e 100644 --- a/.gitignore +++ b/.gitignore @@ -141,3 +141,4 @@ $RECYCLE.BIN/ discordtoken.txt db.json leaguetoken.txt +telegramtoken.txt diff --git a/main.py b/main.py index 6104efc8..9d8d94bb 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ import json import overwatch import league import strings as s +import telegram loop = asyncio.get_event_loop() d_client = discord.Client() @@ -35,6 +36,8 @@ async def overwatch_level_up(timeout): msg = s.overwatch_level_up.format(player=user, level=r["data"]["level"]) # Send the message to the discord channel loop.create_task(d_client.send_message(d_client.get_channel("213655027842154508"), msg)) + # Send the message to the telegram group chat + loop.create_task(telegram.send_message(msg, -2141322)) # Update database db[player]["overwatch"]["level"] = r["data"]["level"] f = open("db.json", "w") @@ -70,6 +73,8 @@ async def league_rank_change(timeout): division=r["entries"][0]["division"]) # Send the message to the discord channel loop.create_task(d_client.send_message(d_client.get_channel("213655027842154508"), msg)) + # Send the message to the telegram group chat + loop.create_task(telegram.send_message(msg, -2141322)) # Update database db[player]["league"]["tier"] = tier_number db[player]["league"]["division"] = roman_number @@ -78,7 +83,9 @@ async def league_rank_change(timeout): f.close() print("Check for League of Legends completed.") +print("Added Overwatch to the queue.") loop.create_task(overwatch_level_up(900)) +print("Added League of Legends to the queue.") loop.create_task(league_rank_change(900)) try: diff --git a/telegram.py b/telegram.py new file mode 100644 index 00000000..40487dcd --- /dev/null +++ b/telegram.py @@ -0,0 +1,23 @@ +import requests +import asyncio +import functools +loop = asyncio.get_event_loop() + +# Load Telegram API key from the leaguetoken.txt file +file = open("telegram.txt", "r") +token = file.read() +file.close() + +# Send a message +async def send_message(msg: str, to: int): + # Send the message + params = { + "chat_id": to, + "text": msg + } + r = loop.run_in_executor(None, functools.partial(requests.get, params=params), + "https://api.telegram.org/bot{token}/sendMessage".format(token=token)) + if r.status_code == 200: + return + else: + raise Exception("Qualcosa รจ andato storto su Telegram. Ops.")