mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Added telegram<->discord bridge
This commit is contained in:
parent
4de4905d18
commit
cc30868574
2 changed files with 56 additions and 4 deletions
47
basicbot.py
47
basicbot.py
|
@ -1,11 +1,15 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import json
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
import telegram
|
import telegram
|
||||||
import random
|
import random
|
||||||
import datetime
|
import datetime
|
||||||
|
import async_timeout
|
||||||
|
import aiohttp
|
||||||
|
import royalbotconfig
|
||||||
|
|
||||||
|
b = telegram.Bot(royalbotconfig.telegram_token)
|
||||||
b = telegram.Bot("lul")
|
|
||||||
|
|
||||||
|
|
||||||
async def diario(bot, update, arguments):
|
async def diario(bot, update, arguments):
|
||||||
|
@ -68,7 +72,46 @@ Sintassi: `/help [comando]`"""
|
||||||
await update.message.chat.send_message(bot, "⚠ Il comando specificato non esiste.")
|
await update.message.chat.send_message(bot, "⚠ Il comando specificato non esiste.")
|
||||||
|
|
||||||
|
|
||||||
|
async def discord(bot, update, arguments):
|
||||||
|
"""Manda un messaggio a #chat di Discord.
|
||||||
|
|
||||||
|
Sintassi: `/discord <messaggio>`"""
|
||||||
|
# TODO: create a discord module
|
||||||
|
# Send a message through a Discord webhook
|
||||||
|
# Message to send
|
||||||
|
if len(arguments) == 0:
|
||||||
|
await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/discord <messaggio>`")
|
||||||
|
return
|
||||||
|
username = f"{update.message.sent_from}"
|
||||||
|
message = " ".join(arguments)
|
||||||
|
# Parameters to send
|
||||||
|
params = {
|
||||||
|
# TODO: show the message sender's Discord username
|
||||||
|
"content": f"{username}: {message}"
|
||||||
|
}
|
||||||
|
# Headers to send
|
||||||
|
headers = {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
# Request timeout is 10 seconds.
|
||||||
|
with async_timeout.timeout(10):
|
||||||
|
# Create a new session for each request.
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
# Send the request to the Discord webhook
|
||||||
|
async with session.request("POST", royalbotconfig.discord_webhook, data=json.dumps(params), headers=headers) as response:
|
||||||
|
# Check if the request was successful
|
||||||
|
if response.status != 204:
|
||||||
|
# Request failed
|
||||||
|
# Answer on Telegram
|
||||||
|
await update.message.chat.send_message(bot, "⚠ L'invio del messaggio è fallito. Oops!")
|
||||||
|
# TODO: handle Discord webhooks errors
|
||||||
|
raise Exception("Qualcosa è andato storto durante l'invio del messaggio a Discord.")
|
||||||
|
# Answer on Telegram
|
||||||
|
await update.message.chat.send_message(bot, "Richiesta inviata.")
|
||||||
|
|
||||||
|
|
||||||
b.commands["leggi"] = leggi
|
b.commands["leggi"] = leggi
|
||||||
b.commands["diario"] = diario
|
b.commands["diario"] = diario
|
||||||
|
b.commands["discord"] = discord
|
||||||
b.commands["help"] = help
|
b.commands["help"] = help
|
||||||
b.run()
|
b.run()
|
13
telegram.py
13
telegram.py
|
@ -130,13 +130,13 @@ class Bot:
|
||||||
async def api_request(self, endpoint, t=10, **params):
|
async def api_request(self, endpoint, t=10, **params):
|
||||||
"""Send a request to the Telegram API at the specified endpoint."""
|
"""Send a request to the Telegram API at the specified endpoint."""
|
||||||
# Request timeout is 10 seconds.
|
# Request timeout is 10 seconds.
|
||||||
|
# TODO: get rid of that t variable
|
||||||
with async_timeout.timeout(t):
|
with async_timeout.timeout(t):
|
||||||
# Create a new session for each request.
|
# Create a new session for each request.
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
# Send the request to the Telegram API
|
# Send the request to the Telegram API
|
||||||
token = self.token
|
token = self.token
|
||||||
async with session.request("GET", f"https://api.telegram.org/bot{token}/{endpoint}",
|
async with session.request("GET", f"https://api.telegram.org/bot{token}/{endpoint}", params=params) as response:
|
||||||
params=params) as response:
|
|
||||||
# Check for errors in the request
|
# Check for errors in the request
|
||||||
if response.status != 200:
|
if response.status != 200:
|
||||||
raise TelegramAPIError(f"Request returned {response.status} {response.reason}")
|
raise TelegramAPIError(f"Request returned {response.status} {response.reason}")
|
||||||
|
@ -237,6 +237,15 @@ class User:
|
||||||
else:
|
else:
|
||||||
self.username = None
|
self.username = None
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
if self.username is not None:
|
||||||
|
return f"@{self.username}"
|
||||||
|
else:
|
||||||
|
if self.last_name is not None:
|
||||||
|
return f"{self.first_name} {self.last_name}"
|
||||||
|
else:
|
||||||
|
return self.first_name
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if self.username is not None:
|
if self.username is not None:
|
||||||
return f"<User {self.username}>"
|
return f"<User {self.username}>"
|
||||||
|
|
Loading…
Reference in a new issue