import asyncio import sys, random import aiohttp as aiohttp import traceback from typing import Sequence from depytg import types, methods, errors HASHTAGS = { "#TeamElio": ( 'https://s9.postimg.cc/p89n5hzz3/photo_2018-01-29_21-47-01.jpg', 'https://s9.postimg.cc/hsadjpjzj/photo_2018-01-29_21-47-11.jpg', 'https://s9.postimg.cc/50w7d7xcv/photo_2018-01-29_21-47-17.jpg', 'https://s9.postimg.cc/iht5w2s8v/photo_2018-01-29_21-47-24.jpg', 'https://s9.postimg.cc/dvx1nqtv3/photo_2018-01-29_21-47-39.jpg', ), "#TeamOliver": ( 'http://filmmakermagazine.com/wp-content/uploads/2017/10/t-call-me-by-your-name.jpg', 'https://s9.postimg.cc/r02m0f6hb/photo_2018-01-29_21-47-32.jpg', ), "#CuloDiElio": ( 'https://s9.postimg.cc/dvx1nqtv3/photo_2018-01-29_21-47-39.jpg', 'https://s9.postimg.cc/iht5w2s8v/photo_2018-01-29_21-47-24.jpg', ) } HELP_TEXT = """A bot that sends a picture of Elio whenever somebody types #TeamElio Made with ❤ by @DepauTM 🏳️‍🌈 Source code: https://git.depau.eu/Depau/CallMeByYourBot Based on DepyTG: https://github.com/Depau/DepyTG""" 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.reply_to_message_id = msg.message_id send_msg["disable_web_page_preview"] = True await send_msg.async_call(self.session, self.token) async def parse_message(self, msg: types.Message): if not 'text' in msg: return if "/help" in msg.text: if "group" in msg.chat.type: username = self.me.username if msg.text == "/help@{}".format(username): await self.send_help(msg) elif msg.text == "/help": await self.send_help(msg) for hashtag in HASHTAGS: if hashtag.lower() in msg.text.lower(): await methods.sendChatAction(msg.chat.id, 'upload_photo').async_call(self.session, self.token) send_photo = methods.sendPhoto(chat_id=msg.chat.id) send_photo.reply_to_message_id = msg.message_id send_photo.photo = random.choice(HASHTAGS[hashtag]) send_photo.caption = hashtag await send_photo.async_call(self.session, self.token) async def parse_update(self, update: types.Update) -> int: print(update) print() for i in ('message', 'edited_message', 'channel_post', 'edited_channel_post'): if i in update: asyncio.ensure_future(self.parse_message(update[i])) break return update.update_id async def on_updates(self, updates: Sequence[types.Update]) -> int: max_id = 0 for u in updates: try: upd_id = await self.parse_update(u) if upd_id > max_id: max_id = upd_id except errors.TelegramError: traceback.print_exc(file=sys.stderr) 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) async def run(self): self._offset = 0 self.me = await methods.getMe().async_call(self.session, self.token) try: while True: self._offset = await self.mainloop(self._offset) + 1 except KeyboardInterrupt: pass 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()