mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Added /diario to the basic bot + other things
This commit is contained in:
parent
e63aaab424
commit
6a75d760e0
2 changed files with 27 additions and 6 deletions
19
basicbot.py
19
basicbot.py
|
@ -2,10 +2,21 @@ import asyncio
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
import telegram
|
import telegram
|
||||||
|
|
||||||
b = telegram.Bot("ciao pizza")
|
b = telegram.Bot("hidden")
|
||||||
|
|
||||||
async def print_message(bot, update):
|
async def diario(bot, update, arguments):
|
||||||
print(update.message.content)
|
# Sì, ho copiato la funzione dal bot vecchio
|
||||||
|
if len(arguments) > 0:
|
||||||
|
entry = " ".join(arguments)
|
||||||
|
if entry.isprintable():
|
||||||
|
entry = entry.replace("\n", " ")
|
||||||
|
time = update.message.date.timestamp()
|
||||||
|
# TODO: add better file handling
|
||||||
|
fdiario = open("diario.txt", "a")
|
||||||
|
fdiario.write(f"{int(time)}|{entry}\n")
|
||||||
|
fdiario.close()
|
||||||
|
del fdiario
|
||||||
|
await update.message.chat.send_message(bot, "Aggiunto al diario!")
|
||||||
|
|
||||||
b.commands["echo"] = print_message
|
b.commands["diario"] = diario
|
||||||
b.run()
|
b.run()
|
14
telegram.py
14
telegram.py
|
@ -60,10 +60,15 @@ class Bot:
|
||||||
# Add the chat to the chat list
|
# Add the chat to the chat list
|
||||||
if update.message.chat not in self.chats:
|
if update.message.chat not in self.chats:
|
||||||
self.chats.append(update.message.chat)
|
self.chats.append(update.message.chat)
|
||||||
|
else:
|
||||||
|
# Replace the chat object in the update with the correct one
|
||||||
|
update.message.chat = self.chats[self.chats.index(update.message.chat)]
|
||||||
# Add the user to the chat
|
# Add the user to the chat
|
||||||
chat = self.find_chat(update.message.chat.chat_id)
|
chat = self.find_chat(update.message.chat.chat_id)
|
||||||
if update.message.sent_from not in chat.users:
|
if update.message.sent_from not in chat.users:
|
||||||
chat.users.append(update.message.sent_from)
|
chat.users.append(update.message.sent_from)
|
||||||
|
else:
|
||||||
|
update.message.sent_from = chat.users[chat.users.index(update.message.sent_from)]
|
||||||
# Add / edit the message to the message list
|
# Add / edit the message to the message list
|
||||||
if not update.message.edited:
|
if not update.message.edited:
|
||||||
chat.messages.append(update.message)
|
chat.messages.append(update.message)
|
||||||
|
@ -77,9 +82,11 @@ class Bot:
|
||||||
# Check if a command can be run
|
# Check if a command can be run
|
||||||
# TODO: use message entities?
|
# TODO: use message entities?
|
||||||
if isinstance(update.message.content, str) and update.message.content.startswith("/"):
|
if isinstance(update.message.content, str) and update.message.content.startswith("/"):
|
||||||
command = update.message.content.split(" ")[0].lstrip("/")
|
split_msg = update.message.content.split(" ")
|
||||||
|
command = split_msg[0].lstrip("/")
|
||||||
if command in self.commands:
|
if command in self.commands:
|
||||||
loop.create_task(self.commands[command](self, update))
|
arguments = split_msg[1:]
|
||||||
|
loop.create_task(self.commands[command](bot=self, update=update, arguments=arguments))
|
||||||
# Update message status if a service message is received
|
# Update message status if a service message is received
|
||||||
if isinstance(update.message.content, ServiceMessage):
|
if isinstance(update.message.content, ServiceMessage):
|
||||||
# New user in chat
|
# New user in chat
|
||||||
|
@ -207,6 +214,9 @@ class Chat:
|
||||||
if msg.msg_id == msg_id:
|
if msg.msg_id == msg_id:
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
async def send_message(self, bot, text, **params):
|
||||||
|
"""Send a message in the chat through the bot object."""
|
||||||
|
await bot.api_request("sendMessage", text=text, chat_id=self.chat_id, **params)
|
||||||
|
|
||||||
|
|
||||||
class User:
|
class User:
|
||||||
|
|
Loading…
Reference in a new issue