1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Updated basicbot to use the new methods

This commit is contained in:
Steffo 2017-03-09 12:51:45 +01:00
parent 90b344dab8
commit f583072edc

View file

@ -1,6 +1,4 @@
import asyncio import asyncio
import json
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
import telegram import telegram
import random import random
@ -8,6 +6,7 @@ import datetime
import async_timeout import async_timeout
import aiohttp import aiohttp
import royalbotconfig import royalbotconfig
import json
b = telegram.Bot(royalbotconfig.telegram_token) b = telegram.Bot(royalbotconfig.telegram_token)
@ -40,7 +39,7 @@ Puoi visualizzare il diario [qui](https://royal.steffo.me/diario.htm), leggere u
Sintassi: `/leggi <random | numerofrase>`""" Sintassi: `/leggi <random | numerofrase>`"""
if len(arguments) == 0 or len(arguments) > 1: if len(arguments) == 0 or len(arguments) > 1:
await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/leggi <random | numerofrase>`") await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/leggi <random | numerofrase>`")
return return
file = open("diario.txt", "r") file = open("diario.txt", "r")
entries = file.read().split("\n") entries = file.read().split("\n")
@ -52,7 +51,7 @@ Sintassi: `/leggi <random | numerofrase>`"""
entry = entries[entry_number].split("|", 1) entry = entries[entry_number].split("|", 1)
date = datetime.datetime.fromtimestamp(entry[0]).isoformat() date = datetime.datetime.fromtimestamp(entry[0]).isoformat()
text = entry[1] text = entry[1]
await update.message.chat.send_message(bot, f"Frase #{entry_number} | {date}\n{text}") await update.message.reply(bot, f"Frase #{entry_number} | {date}\n{text}")
async def help(bot, update, arguments): async def help(bot, update, arguments):
@ -60,14 +59,14 @@ async def help(bot, update, arguments):
Sintassi: `/help [comando]`""" Sintassi: `/help [comando]`"""
if len(arguments) == 0: if len(arguments) == 0:
await update.message.chat.send_message(bot, help.__doc__) await update.message.reply(bot, help.__doc__)
elif len(arguments) > 1: elif len(arguments) > 1:
await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/help [comando]`") await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/help [comando]`")
else: else:
if arguments[0] in b.commands: if arguments[0] in b.commands:
await update.message.chat.send_message(bot, b.commands[arguments[0]].__doc__) await update.message.reply(bot, b.commands[arguments[0]].__doc__)
else: else:
await update.message.chat.send_message(bot, "⚠ Il comando specificato non esiste.") await update.message.reply(bot, "⚠ Il comando specificato non esiste.")
async def discord(bot, update, arguments): async def discord(bot, update, arguments):
@ -80,7 +79,7 @@ Sintassi: `/discord <messaggio>`"""
if len(arguments) == 0: if len(arguments) == 0:
await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/discord <messaggio>`") await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/discord <messaggio>`")
return return
username = f"{update.message.sent_from}" username = str(update.message.sent_from)
message = " ".join(arguments) message = " ".join(arguments)
# Parameters to send # Parameters to send
params = { params = {
@ -101,15 +100,16 @@ Sintassi: `/discord <messaggio>`"""
if response.status != 204: if response.status != 204:
# Request failed # Request failed
# Answer on Telegram # Answer on Telegram
await update.message.chat.send_message(bot, "⚠ L'invio del messaggio è fallito. Oops!") await update.message.reply(bot, "⚠ L'invio del messaggio è fallito. Oops!")
# TODO: handle Discord webhooks errors # TODO: handle Discord webhooks errors
raise Exception("Qualcosa è andato storto durante l'invio del messaggio a Discord.") raise Exception("Qualcosa è andato storto durante l'invio del messaggio a Discord.")
# Answer on Telegram # Answer on Telegram
await update.message.chat.send_message(bot, "Richiesta inviata.") await update.message.reply(bot, "Richiesta inviata.")
b.commands["leggi"] = leggi if __name__ == "__main__":
b.commands["diario"] = diario b.commands["leggi"] = leggi
b.commands["discord"] = discord b.commands["diario"] = diario
b.commands["help"] = help b.commands["discord"] = discord
b.run() b.commands["help"] = help
b.run()