Advertisement
disk6969

Untitled

Apr 17th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. from telethon import events, types,functions
  2. from telethon.sync import TelegramClient
  3. from telethon.tl.types import InputMediaPoll
  4. import random, asyncio
  5. from telethon.tl.functions.channels import GetFullChannelRequest
  6.  
  7. # Your API credentials
  8. api_id = ''
  9. api_hash = ''
  10. session_name = ''
  11.  
  12. # Create a new TelegramClient instance
  13. client = TelegramClient(session_name, api_id, api_hash)
  14.  
  15. #Join request to a channel which the bot is admin of will trigger sending a poll to the user trying to join
  16. @client.on(events.Raw(types.UpdateBotChatInviteRequester))
  17. async def join_requested(event):
  18.     try:
  19.         print("\nNEW JOIN REQUEST\n\n" + event.stringify() +"\n")
  20.         entity = await client.get_entity(event.peer)
  21.         invite = (await client(GetFullChannelRequest(entity))).full_chat.exported_invite.link
  22.         channel_name = entity.title  
  23.         channel_id = event.peer.channel_id
  24.         user_id = event.user_id
  25.         print("User ID:", user_id)
  26.         print("Channel Name:", channel_name)
  27.         print("Channel ID:", channel_id)
  28.         print("Channel Link:", invite)
  29.         newsub = f'\nNew sub!\n\nUser ID: {user_id}\nChannel ID: {channel_id}\nChannel Name: {channel_name}\n{invite}\n'
  30.     except Exception as e:
  31.         print("Error fetching channel entity:", e)
  32.  
  33.     # Define the poll question and answers
  34.     question = 'How much is 3+1?'
  35.     answers = [types.PollAnswer('1', b'1'), types.PollAnswer('2', b'2'),types.PollAnswer('3', b'3'), types.PollAnswer('4', b'4'), ]
  36.     random.shuffle(answers)
  37.  
  38.     # Create the poll object
  39.     poll = types.Poll(
  40.         id=653342256788,
  41.         question=question,
  42.         answers=answers,
  43.         public_voters=False,
  44.         quiz=True,
  45.         multiple_choice=False,
  46.     )
  47.  
  48.     # Create the InputMediaPoll object
  49.     media = types.InputMediaPoll(
  50.         poll=poll,
  51.         correct_answers=[b'4']  # Correct answer(s)
  52.     )
  53.  
  54.     # Send the poll
  55.     await client.send_message(user_id, f'To join **{channel_name}**, successfully answer the following question:')
  56.     my_poll = await client.send_file(event.user_id, media)
  57.     poll_id = my_poll.poll.poll.id
  58.  
  59.     wait_vote = asyncio.Event()
  60.  
  61.     @client.on(events.Raw(types.UpdateMessagePoll, func=lambda e: print('check', user_id, e.poll_id == poll_id) or e.poll_id == poll_id))
  62.     async def check_answer(event):
  63.         wait_vote.set()
  64.         print("\nPOLL RESULT\n\n" + event.stringify() + "\n")
  65.         print(f"Poll ID: {event.poll_id}")
  66.         options_and_voters = {answer.text: voters.voters for answer, voters in zip(event.poll.answers, event.results.results)}
  67.         print(options_and_voters)
  68.         if options_and_voters['4'] == 1:
  69.             # Approve join request
  70.             try:
  71.                 await client.send_message(user_id, "You seem like a clever person, I'm going to let you in 😏")
  72.                 print('Success')
  73.                 print(newsub)
  74.                 await client(functions.messages.HideChatJoinRequestRequest(channel_id, user_id, approved=True))
  75.             except Exception as e:
  76.                 print(e)
  77.         else:
  78.             try:
  79.                 await client.send_message(user_id, "Womp womp woooooomp. Brush yourself off and try again.")
  80.                 print('Fail')
  81.                 await client(functions.messages.HideChatJoinRequestRequest(channel_id, user_id, approved=False))
  82.             except Exception as e:
  83.                 print(e)
  84.     try:
  85.         await asyncio.wait_for(wait_vote.wait(), timeout=10 * 60)
  86.     except TimeoutError:
  87.         await client.send_message(user_id, "Too slow, try again.")
  88.     finally:
  89.         print('vote reached, clean handler')
  90.         client.remove_event_handler(check_answer)
  91.  
  92. async def main():
  93.     await client.start()
  94.     print('\nLogged in')
  95.     await client.run_until_disconnected()
  96.    
  97. if __name__ == '__main__':
  98.     asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement