1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Send messages to telegram

This commit is contained in:
Steffo 2016-08-13 14:19:41 +02:00
parent 680c22a507
commit 0a98f8edaa
3 changed files with 31 additions and 0 deletions

1
.gitignore vendored
View file

@ -141,3 +141,4 @@ $RECYCLE.BIN/
discordtoken.txt
db.json
leaguetoken.txt
telegramtoken.txt

View file

@ -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:

23
telegram.py Normal file
View file

@ -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.")