genstr.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import asyncio
  2. from asyncio.exceptions import TimeoutError
  3. from pyrogram import Client, filters
  4. from pyrogram.errors import (PhoneCodeExpired, PhoneCodeInvalid,
  5. PhoneNumberInvalid, SessionPasswordNeeded)
  6. from wbb import app
  7. from wbb.core.decorators.errors import capture_err
  8. """
  9. Credits:
  10. This module belongs to https://github.com/AbirHasan2005/TG-String-Session/blob/main/genStr.py
  11. """
  12. __MODULE__ = "GenStr"
  13. __HELP__ = """
  14. This Module Can Be Used To Generate Session String For A Userbot.
  15. Send **/genstr** Command To The Bot In Private And Follow Instructions."""
  16. @app.on_message(filters.command("genstr") & filters.private)
  17. @capture_err
  18. async def genstr(_, message):
  19. chat = message.chat
  20. while True:
  21. number = await app.ask(
  22. chat.id, "Send Your Phone Number In International Format."
  23. )
  24. if not number.text:
  25. continue
  26. phone = number.text.strip()
  27. if phone.startswith("/"):
  28. continue
  29. confirm = await app.ask(
  30. chat.id, f'`Is "{phone}" correct?` \n\nSend: `y`\nSend: `n`'
  31. )
  32. if confirm.text.startswith("/"):
  33. continue
  34. if "y" in confirm.text.lower():
  35. break
  36. try:
  37. temp_client = Client(
  38. ":memory:", api_id=6, api_hash="eb06d4abfb49dc3eeb1aeb98ae0f581e"
  39. )
  40. except Exception as e:
  41. await app.send_message(chat.id, f"**ERROR:** `{str(e)}`")
  42. return
  43. try:
  44. await temp_client.connect()
  45. except ConnectionError:
  46. await temp_client.disconnect()
  47. await temp_client.connect()
  48. try:
  49. code = await temp_client.send_code(phone)
  50. await asyncio.sleep(2)
  51. except PhoneNumberInvalid:
  52. return await message.reply_text("Phone Number is Invalid")
  53. try:
  54. otp = await app.ask(
  55. chat.id,
  56. (
  57. "An OTP is sent to your phone number, "
  58. "Please enter OTP in `1 2 3 4 5` format. __(Space between each numbers!)__"
  59. ),
  60. timeout=300,
  61. )
  62. except TimeoutError:
  63. return await message.reply_text(
  64. "Time limit reached of 5 min. Process Cancelled."
  65. )
  66. otp_code = otp.text
  67. try:
  68. await temp_client.sign_in(
  69. phone, code.phone_code_hash, phone_code=" ".join(str(otp_code))
  70. )
  71. except PhoneCodeInvalid:
  72. return await message.reply_text("Invalid OTP.")
  73. except PhoneCodeExpired:
  74. return await message.reply_text("OTP is Expired.")
  75. except SessionPasswordNeeded:
  76. try:
  77. two_step_code = await app.ask(
  78. chat.id,
  79. "Your account have Two-Step Verification.\nPlease enter your Password.",
  80. timeout=300,
  81. )
  82. except TimeoutError:
  83. return await message.reply_text("Time limit reached of 5 min.")
  84. new_code = two_step_code.text
  85. try:
  86. await temp_client.check_password(new_code)
  87. except Exception as e:
  88. return await message.reply_text(f"**ERROR:** `{str(e)}`")
  89. except Exception as e:
  90. return await app.send_message(chat.id, f"**ERROR:** `{str(e)}`")
  91. try:
  92. session_string = await temp_client.export_session_string()
  93. await temp_client.disconnect()
  94. await app.send_message(
  95. chat.id,
  96. text=f"**HERE IS YOUR STRING SESSION:**\n```{session_string}```",
  97. )
  98. except Exception as e:
  99. await app.send_message(chat.id, f"**ERROR:** `{str(e)}`")