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

Multiple things

Command functions must be async
Bot.run() to run the bot automatically
Get the api key from $royalbottelegram
This commit is contained in:
Steffo 2017-02-27 23:03:38 +01:00
parent 21c8753963
commit fb459deff2

View file

@ -3,6 +3,7 @@ loop = asyncio.get_event_loop()
import aiohttp import aiohttp
import async_timeout import async_timeout
import datetime import datetime
import os
class TelegramAPIError(Exception): class TelegramAPIError(Exception):
pass pass
@ -27,6 +28,14 @@ class Bot:
def __hash__(self): def __hash__(self):
return hash(self.token) return hash(self.token)
async def run(self):
"""Run the bot automatically."""
while True:
loop.run_until_complete(self.get_updates())
for u in self.updates:
loop.create_task(self.parse_update(u))
self.updates = list()
async def update_bot_data(self): async def update_bot_data(self):
"""Update self.user_data with the latest information from /getMe.""" """Update self.user_data with the latest information from /getMe."""
data = await self.api_request("getMe") data = await self.api_request("getMe")
@ -70,7 +79,7 @@ class Bot:
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("/") command = update.message.content.split(" ")[0].lstrip("/")
if command in self.commands: if command in self.commands:
self.commands[command](self, update) loop.create_task(self.commands[command](self, update))
# 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
@ -361,14 +370,5 @@ class Venue:
raise NotImplementedError("Not yet.") raise NotImplementedError("Not yet.")
def legoflegend(bot, update): b = Bot(os.environ["royalbottelegram"])
print("Message received: " + repr(update)) loop.run_until_complete(b.run())
b = Bot("369842636:AAEal9io4zmgc73i6GlJSLzkccG__A4sYbo")
b.commands["lol"] = legoflegend
while True:
loop.run_until_complete(b.get_updates())
for u in b.updates:
loop.create_task(b.parse_update(u))
b.updates = list()
print("Completed.")