Port bot to asyncio

This commit is contained in:
Davide Depau 2018-06-24 15:47:23 +02:00
parent fd692c4df7
commit 5f898f22bb
Signed by: depau
GPG key ID: C7D999B6A55EFE86
2 changed files with 74 additions and 61 deletions

View file

@ -1,7 +1,9 @@
import asyncio
import sys, random import sys, random
import aiohttp as aiohttp
import traceback import traceback
import time from typing import Sequence
from typing import Sequence, AnyStr
from depytg import types, methods, errors from depytg import types, methods, errors
HASHTAGS = { HASHTAGS = {
@ -29,26 +31,33 @@ Source code: https://git.depau.eu/Depau/CallMeByYourBot
Based on DepyTG: https://github.com/Depau/DepyTG""" Based on DepyTG: https://github.com/Depau/DepyTG"""
def send_help(token: AnyStr, msg: types.Message): class CallMeByYourBot:
def __init__(self, token: str, loop: asyncio.AbstractEventLoop, session: aiohttp.ClientSession):
self.token = token
self.loop = loop
self.session = session
self._offset = 0
self.me = None
async def send_help(self, msg: types.Message):
send_msg = methods.sendMessage(msg.chat.id, HELP_TEXT) send_msg = methods.sendMessage(msg.chat.id, HELP_TEXT)
send_msg.reply_to_message_id = msg.message_id send_msg.reply_to_message_id = msg.message_id
send_msg["disable_web_page_preview"] = True send_msg["disable_web_page_preview"] = True
send_msg(token) await send_msg.async_call(self.session, self.token)
async def parse_message(self, msg: types.Message):
def parse_message(token: AnyStr, msg: types.Message):
if not 'text' in msg: if not 'text' in msg:
return return
if "/help" in msg.text: if "/help" in msg.text:
if "group" in msg.chat.type: if "group" in msg.chat.type:
username = methods.getMe()(token).username username = self.me.username
if msg.text == "/help@{}".format(username): if msg.text == "/help@{}".format(username):
send_help(token, msg) await self.send_help(msg)
elif msg.text == "/help": elif msg.text == "/help":
send_help(token, msg) await self.send_help(msg)
for hashtag in HASHTAGS: for hashtag in HASHTAGS:
if hashtag.lower() in msg.text.lower(): if hashtag.lower() in msg.text.lower():
@ -56,24 +65,23 @@ def parse_message(token: AnyStr, msg: types.Message):
send_photo.reply_to_message_id = msg.message_id send_photo.reply_to_message_id = msg.message_id
send_photo.photo = random.choice(HASHTAGS[hashtag]) send_photo.photo = random.choice(HASHTAGS[hashtag])
send_photo.caption = hashtag send_photo.caption = hashtag
send_photo(token) resp = await send_photo.async_call(self.session, self.token)
async def parse_update(self, update: types.Update) -> int:
def parse_update(token: AnyStr, update: types.Update) -> int:
print(update) print(update)
print() print()
for i in ('message', 'edited_message', 'channel_post', 'edited_channel_post'): for i in ('message', 'edited_message', 'channel_post', 'edited_channel_post'):
if i in update: if i in update:
parse_message(token, update[i]) asyncio.ensure_future(self.parse_message(update[i]))
break break
return update.update_id return update.update_id
async def on_updates(self, updates: Sequence[types.Update]) -> int:
def on_updates(token: AnyStr, updates: Sequence[types.Update]) -> int:
max_id = 0 max_id = 0
for u in updates: for u in updates:
try: try:
upd_id = parse_update(token, u) upd_id = await self.parse_update(u)
if upd_id > max_id: if upd_id > max_id:
max_id = upd_id max_id = upd_id
@ -82,23 +90,29 @@ def on_updates(token: AnyStr, updates: Sequence[types.Update]) -> int:
return max_id return max_id
async def mainloop(self, offset: int = 0) -> int:
updates = await methods.getUpdates(offset).async_call(self.session, self.token)
return await self.on_updates(updates)
def mainloop(token: AnyStr, offset: int = 0) -> int: async def run(self):
updates = methods.getUpdates(offset)(token) self._offset = 0
return on_updates(token, updates) self.me = await methods.getMe().async_call(self.session, self.token)
def main():
token = sys.argv[1]
prev_offset = 0
try: try:
while True: while True:
prev_offset = mainloop(token, prev_offset) + 1 self._offset = await self.mainloop(self._offset) + 1
time.sleep(0.2)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
if __name__ == "__main__": def main():
token = sys.argv[1]
loop = asyncio.get_event_loop()
session = aiohttp.ClientSession(loop=loop)
bot = CallMeByYourBot(token, loop, session)
loop.run_until_complete(bot.run())
loop.close()
if __name__ == '__main__':
main() main()

View file

@ -2,15 +2,14 @@ from setuptools import setup
setup( setup(
name='Call Me By Your Bot', name='Call Me By Your Bot',
version='0.1', version='0.2',
packages=['callmebyyourbot'], packages=['callmebyyourbot'],
url='https://github.com/Depaulicious/', url='https://git.depau.eu/Depau/CallMeByYourBot',
license='', license='',
author='Davide Depau', author='Davide Depau',
author_email='davide@depau.eu', author_email='davide@depau.eu',
description='A Telegram bot that replies with a picture of Elio when #TeamElio is sent', description='A Telegram bot that replies with a picture of Elio when #TeamElio is sent',
dependency_links=["https://github.com/Depaulicious/DepyTG/archive/wip.zip"], requires=["DepyTG[asyncio]", "aiohttp"],
requires=["DepyTG", "requests"],
entry_points={ entry_points={
'console_scripts': ['callmebyyourbot=callmebyyourbot:main'], 'console_scripts': ['callmebyyourbot=callmebyyourbot:main'],
} }