You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4KB

  1. import asyncio
  2. import logging
  3. import settings
  4. import time
  5. import validators
  6. from aiohttp import web
  7. from telethon import TelegramClient, events
  8. TANNER = 79316791
  9. bot = TelegramClient('bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN)
  10. routes = web.RouteTableDef()
  11. logging.basicConfig(level=logging.INFO)
  12. data = ''
  13. data_time = 0
  14. @bot.on(events.NewMessage)
  15. async def new_message(event):
  16. global data, data_time
  17. if not event.raw_text:
  18. await event.reply('no')
  19. logging.info('No text found')
  20. return
  21. logging.info('Message: ' + event.raw_text)
  22. if event.sender.id != TANNER:
  23. await event.reply('no')
  24. logging.info('Message not from Tanner')
  25. return
  26. data = event.raw_text
  27. data_time = time.time()
  28. await event.reply('ok')
  29. @routes.get('/')
  30. async def index(request):
  31. if 'bot' in request.headers.get('User-Agent', '').lower():
  32. raise web.HTTPForbidden()
  33. if time.time() - data_time > 3600:
  34. logging.info('Time expired, showing green ocean water')
  35. return web.FileResponse(path='greenoceanwater.jpg', status=200)
  36. if validators.domain(data):
  37. raise web.HTTPFound('http://' + data)
  38. elif validators.url(data):
  39. raise web.HTTPFound(data)
  40. else:
  41. return web.Response(text=data)
  42. app = web.Application()
  43. app.add_routes(routes)
  44. web.run_app(app)