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

29 lines
843 B
Python
Raw Permalink Normal View History

2016-08-13 12:19:41 +00:00
import requests
import asyncio
import functools
loop = asyncio.get_event_loop()
2016-08-13 12:21:27 +00:00
# Load Telegram API key from the telegramtoken.txt file
2016-08-13 12:24:17 +00:00
file = open("telegramtoken.txt", "r")
2016-08-13 12:19:41 +00:00
token = file.read()
file.close()
2017-02-07 21:35:19 +00:00
class TelegramError(Exception):
pass
2016-08-13 12:19:41 +00:00
# Send a message
async def send_message(msg: str, to: int):
2016-08-13 14:23:58 +00:00
print("[Telegram] Sending a message: " + msg)
2016-08-13 12:19:41 +00:00
# Send the message
params = {
"chat_id": to,
2016-08-13 14:51:26 +00:00
"text": msg,
"parse_mode": "Markdown"
2016-08-13 12:19:41 +00:00
}
2016-08-13 14:00:35 +00:00
r = await loop.run_in_executor(None, functools.partial(requests.get, params=params),
"https://api.telegram.org/bot{token}/sendMessage".format(token=token))
2016-08-13 12:19:41 +00:00
if r.status_code == 200:
return
else:
2017-02-07 21:35:19 +00:00
raise TelegramError("Something went wrong in the Telegram request: {}".format(r.json()["description"]))