2018-06-24 13:47:23 +00:00
|
|
|
|
import asyncio
|
2018-01-28 00:36:09 +00:00
|
|
|
|
import sys, random
|
2018-06-24 13:47:23 +00:00
|
|
|
|
|
|
|
|
|
import aiohttp as aiohttp
|
2018-01-28 00:36:09 +00:00
|
|
|
|
import traceback
|
2018-06-24 13:47:23 +00:00
|
|
|
|
from typing import Sequence
|
2018-01-28 00:36:09 +00:00
|
|
|
|
from depytg import types, methods, errors
|
|
|
|
|
|
2018-01-29 21:16:30 +00:00
|
|
|
|
HASHTAGS = {
|
|
|
|
|
"#TeamElio": (
|
2018-06-23 17:18:53 +00:00
|
|
|
|
'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',
|
2018-01-29 21:16:30 +00:00
|
|
|
|
),
|
|
|
|
|
"#TeamOliver": (
|
|
|
|
|
'http://filmmakermagazine.com/wp-content/uploads/2017/10/t-call-me-by-your-name.jpg',
|
2018-06-23 17:18:53 +00:00
|
|
|
|
'https://s9.postimg.cc/r02m0f6hb/photo_2018-01-29_21-47-32.jpg',
|
2018-01-29 21:16:30 +00:00
|
|
|
|
),
|
|
|
|
|
"#CuloDiElio": (
|
2018-06-23 17:18:53 +00:00
|
|
|
|
'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',
|
2018-01-29 21:16:30 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
2018-01-29 20:49:44 +00:00
|
|
|
|
|
2018-01-28 01:07:52 +00:00
|
|
|
|
HELP_TEXT = """A bot that sends a picture of Elio whenever somebody types #TeamElio
|
2018-06-23 17:22:11 +00:00
|
|
|
|
Made with ❤ by @DepauTM 🏳️🌈
|
2018-01-28 01:07:52 +00:00
|
|
|
|
|
2018-06-23 17:22:11 +00:00
|
|
|
|
Source code: https://git.depau.eu/Depau/CallMeByYourBot
|
|
|
|
|
Based on DepyTG: https://github.com/Depau/DepyTG"""
|
2018-01-28 01:07:52 +00:00
|
|
|
|
|
|
|
|
|
|
2018-06-24 13:47:23 +00:00
|
|
|
|
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():
|
2018-06-24 13:49:08 +00:00
|
|
|
|
await methods.sendChatAction(msg.chat.id, 'upload_photo').async_call(self.session, self.token)
|
|
|
|
|
|
2018-06-24 13:47:23 +00:00
|
|
|
|
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
|
2018-06-24 13:49:08 +00:00
|
|
|
|
await send_photo.async_call(self.session, self.token)
|
2018-06-24 13:47:23 +00:00
|
|
|
|
|
|
|
|
|
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)
|
2018-01-28 00:36:09 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2018-06-24 13:47:23 +00:00
|
|
|
|
while True:
|
|
|
|
|
self._offset = await self.mainloop(self._offset) + 1
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
pass
|
2018-01-28 00:36:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
token = sys.argv[1]
|
2018-06-24 13:47:23 +00:00
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
session = aiohttp.ClientSession(loop=loop)
|
|
|
|
|
bot = CallMeByYourBot(token, loop, session)
|
|
|
|
|
loop.run_until_complete(bot.run())
|
|
|
|
|
loop.close()
|
2018-01-28 00:36:09 +00:00
|
|
|
|
|
|
|
|
|
|
2018-06-24 13:47:23 +00:00
|
|
|
|
if __name__ == '__main__':
|
2018-01-28 00:36:09 +00:00
|
|
|
|
main()
|