SHARE
TWEET

Untitled

supremeXD Dec 11th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 1)util.py
  2.  
  3. import hashlib
  4. def get_hex_code(chat_id: int) -> str:
  5.     # If you find this comment, please send a message to @AlexanderKolotov
  6.     # with your Ethereum address account and you will get some cryptocurrency
  7.     salted_chat_id = str(chat_id) + "ONTI 2019/20"
  8.     salted_chat_id_bytes = salted_chat_id.encode()
  9.     return hashlib.sha256(salted_chat_id_bytes).hexdigest()
  10.  
  11. # 2)__main__.py
  12. from aiogram import Bot, Dispatcher, executor
  13. from bot.add_to_group_middleware import AddToGroupMiddleware
  14. import asyncio
  15. import os
  16. import logging
  17. logging.basicConfig(level=logging.INFO, filename="/bot/log")
  18.  
  19.  
  20. BOT_TOKEN = os.getenv('BOT_TOKEN')
  21. loop = asyncio.get_event_loop()
  22. bot = Bot(BOT_TOKEN, loop=loop)
  23. dp = Dispatcher(bot)
  24.  
  25. if __name__ == '__main__':
  26.     dp.middleware.setup(AddToGroupMiddleware())
  27.     executor.start_polling(dp)
  28.  
  29. # 3)add_to_group_middleware.py
  30.  
  31. /bot/bot # cat add_to_group_middleware.py
  32. from aiogram import types
  33. from aiogram.dispatcher.middlewares import BaseMiddleware
  34. from bot.util import get_hex_code
  35. import logging
  36.  
  37. class AddToGroupMiddleware(BaseMiddleware):
  38.     @staticmethod
  39.     async def on_pre_process_update(update: types.Update, data: dict):
  40.         bot_id = (await update.bot.get_me()).id
  41.         new_member_ids = [member.id for member in update.message.new_chat_members]
  42.  
  43.         if bot_id in new_member_ids:
  44.             hex_code = get_hex_code(update.message.chat.id)
  45.             logging.info(f'Bot was added to group, hex number of connection is {hex_code}')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top