Skip to content

Commit e1a00c0

Browse files
committed
using conversation handlers
- Thanks to @usernein - removed caching (will help with security and tos :D) - simpler code and error Handling
1 parent 3d80278 commit e1a00c0

10 files changed

+140
-233
lines changed

psm/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Client Initial."""
2+
import pyromod.listen
23
import logging
34
from configparser import ConfigParser
45
from datetime import datetime

psm/plugins/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"""Plugins Directory."""
1+
"""Plugins Directory."""

psm/plugins/clear_cache.py

-69
This file was deleted.

psm/plugins/dictionaries.py

-4
This file was deleted.

psm/plugins/session_maker.py

+78-102
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,94 @@
1-
from pyrogram import filters
2-
from pyrogram import Client
3-
from pyrogram.errors import FloodWait
4-
from pyrogram import errors
5-
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, ForceReply
1+
from pyrogram import filters, Client, errors
2+
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
3+
from pyromod import listen
64

75
import asyncio
86

97
from psm import psm
10-
from psm.plugins.dictionaries import code_caches, app_ids, app_hashs, passwords
8+
from psm.strings import strings
119

1210

13-
async def client_session(message):
14-
return Client(
15-
":memory:",
16-
api_id=int(app_ids[message.from_user.id]),
17-
api_hash=str(app_hashs[message.from_user.id]),
18-
)
11+
async def client_session(message, api_id, api_hash):
12+
return Client(":memory:", api_id=int(api_id), api_hash=str(api_hash))
1913

2014

21-
@psm.on_message(filters.command("phone"))
22-
async def phone_number(_, message):
23-
try:
24-
app = await client_session(message)
25-
except KeyError:
26-
await message.reply("You did not set Variables correctly, read /start again.")
27-
return
28-
try:
29-
phonenum = message.text.split(None, 1)[1].replace(" ", "")
30-
except IndexError:
31-
await message.reply("Must pass args, example: `/phone +1234578900`")
32-
return
33-
try:
34-
await app.connect()
35-
except ConnectionError:
36-
await app.disconnect()
37-
await app.connect()
38-
try:
39-
sent_code = await app.send_code(phonenum)
40-
except FloodWait as e:
41-
await message.reply(
42-
f"I cannot create session for you.\nYou have a floodwait of: `{e.x} seconds`"
43-
)
44-
return
45-
except errors.exceptions.bad_request_400.PhoneNumberInvalid:
46-
await message.reply(
47-
"Phone number is invalid, Make sure you double check before sending."
48-
)
49-
return
50-
await message.reply(
51-
"send me your code in 25 seconds, make sure you reply to this message and wait for a response.",
52-
reply_markup=ForceReply(True),
53-
)
54-
await asyncio.sleep(25)
55-
try:
56-
await app.sign_in(
57-
phonenum, sent_code.phone_code_hash, code_caches[message.from_user.id]
58-
)
59-
except KeyError:
60-
await message.reply("Timed out, Try again.")
61-
return
62-
except errors.exceptions.unauthorized_401.SessionPasswordNeeded:
15+
@psm.on_message(filters.command("proceed") | ~filters.command("start"))
16+
async def sessions_make(client, message):
17+
apid = await client.ask(message.chat.id, strings.APIID)
18+
aphash = await client.ask(message.chat.id, strings.APIHASH)
19+
phone_token = await client.ask(message.chat.id, strings.PHONETOKEN)
20+
if str(phone_token.text).startswith("+"):
21+
try:
22+
app = await client_session(message, api_id=apid.text, api_hash=aphash.text)
23+
except Exception as err:
24+
await message.reply(strings.ERROR.format(err=err))
25+
return
26+
try:
27+
await app.connect()
28+
except ConnectionError:
29+
await app.disconnect()
30+
await app.connect()
6331
try:
64-
await app.check_password(passwords[message.from_user.id])
65-
except KeyError:
32+
sent_code = await app.send_code(phone_token.text)
33+
except errors.FloodWait as e:
6634
await message.reply(
67-
"You have not set your password in the `/variables`, try doing that before you continue"
35+
f"I cannot create session for you.\nYou have a floodwait of: `{e.x} seconds`"
6836
)
6937
return
70-
except errors.exceptions.bad_request_400.PhoneCodeInvalid:
71-
await message.reply("The code you sent seems Invalid, Try again.")
72-
return
73-
except errors.exceptions.bad_request_400.PhoneCodeExpired:
74-
await message.reply("The Code you sent seems Expired. Try again.")
75-
return
76-
await app.send_message("me", f"```{(await app.export_session_string())}```")
77-
button = InlineKeyboardMarkup(
78-
[
38+
except errors.PhoneNumberInvalid:
39+
await message.reply(strings.INVALIDNUMBER)
40+
return
41+
except errors.ApiIdInvalid:
42+
await message.reply(strings.APIINVALID)
43+
return
44+
ans = await client.ask(message.chat.id, strings.PHONECODE)
45+
46+
try:
47+
await app.sign_in(phone_token.text, sent_code.phone_code_hash, ans.text)
48+
except errors.SessionPasswordNeeded:
49+
pas = await client.ask(message.chat.id, strings.PASSWORD)
50+
try:
51+
await app.check_password(pas.text)
52+
except Exception as err:
53+
await message.reply(strings.ERROR.format(err=err))
54+
return
55+
except errors.PhoneCodeInvalid:
56+
await message.reply(strings.PHONECODEINVALID)
57+
return
58+
except errors.PhoneCodeExpired:
59+
await message.reply(strings.PHONECODEINVALID)
60+
return
61+
await app.send_message("me", f"```{(await app.export_session_string())}```")
62+
button = InlineKeyboardMarkup(
7963
[
80-
InlineKeyboardButton(
81-
"Go to Saved Messages", url=f"tg://user?id={message.from_user.id}"
82-
)
64+
[
65+
InlineKeyboardButton(
66+
"Go to Saved Messages",
67+
url=f"tg://user?id={message.from_user.id}",
68+
)
69+
]
8370
]
84-
]
85-
)
86-
await message.reply(
87-
"All Done! Check your Saved Messages for your Session String.\n\nMake sure you run /clear to clear caches of your variables",
88-
reply_markup=button,
89-
)
90-
91-
92-
@psm.on_message(filters.command("token"))
93-
async def bot_token(_, message):
94-
try:
95-
app = await client_session(message)
96-
except KeyError:
97-
await message.reply("You did not set Variables correctly, read /start again.")
98-
return
99-
try:
100-
bottoken = message.text.split(None, 1)[1].replace(" ", "")
101-
except IndexError:
102-
await message.reply("Must pass args, example: `/token 1234:ABCD1234`")
103-
return
104-
try:
105-
await app.connect()
106-
except ConnectionError:
107-
await app.disconnect()
108-
await app.connect()
109-
try:
110-
await app.sign_in_bot(bottoken)
111-
except errors.exceptions.bad_request_400.AccessTokenInvalid:
71+
)
72+
await message.reply(
73+
strings.DONEPHONE,
74+
reply_markup=button,
75+
)
76+
else:
77+
try:
78+
app = await client_session(message, api_id=apid.text, api_hash=aphash.text)
79+
except Exception as err:
80+
await message.reply(strings.ERROR.format(err=err))
81+
return
82+
try:
83+
await app.connect()
84+
except ConnectionError:
85+
await app.disconnect()
86+
await app.connect()
87+
try:
88+
await app.sign_in_bot(phone_token.text)
89+
except errors.AccessTokenInvalid:
90+
await message.reply(strings.BOTTOKENINVALID)
91+
return
11292
await message.reply(
113-
"BotToken Invalid: make sure you are sending a valid BotToken from @BotFather"
93+
f"**Here is your Bot Session:**\n```{(await app.export_session_string())}```\n\nHappy pyrogramming"
11494
)
115-
return
116-
await message.reply(
117-
f"**Here is your Bot Session:**\n```{(await app.export_session_string())}```\n\nMake sure you run /clear to clear caches of your variables"
118-
)

psm/plugins/start.py

+30-12
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,58 @@
88

99
@psm.on_message(filters.command("start"))
1010
async def alive(_, message):
11-
buttons = [[InlineKeyboardButton('How it works', callback_data='help_1')]]
11+
buttons = [[InlineKeyboardButton("How it works", callback_data="help_1")]]
1212
await message.reply(helptext, reply_markup=InlineKeyboardMarkup(buttons))
1313

1414

1515
@psm.on_callback_query(dynamic_data_filter("help_1"))
1616
async def help_button(_, query):
17-
buttons = [[InlineKeyboardButton('Next', callback_data='help_2')]]
18-
await query.message.edit(helptext1, disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup(buttons))
17+
buttons = [[InlineKeyboardButton("Next", callback_data="help_2")]]
18+
await query.message.edit(
19+
helptext1,
20+
disable_web_page_preview=True,
21+
reply_markup=InlineKeyboardMarkup(buttons),
22+
)
1923
await query.answer()
2024

2125

2226
@psm.on_callback_query(dynamic_data_filter("help_2"))
2327
async def help_button1(_, query):
2428
buttons = [
25-
[InlineKeyboardButton('Previous', callback_data='help_1'),
26-
InlineKeyboardButton('Next', callback_data='help_3')]]
29+
[
30+
InlineKeyboardButton("Previous", callback_data="help_1"),
31+
InlineKeyboardButton("Next", callback_data="help_3"),
32+
]
33+
]
2734
await query.message.edit(helptext2, reply_markup=InlineKeyboardMarkup(buttons))
2835
await query.answer()
2936

3037

3138
@psm.on_callback_query(dynamic_data_filter("help_3"))
3239
async def help_button2(_, query):
3340
buttons = [
34-
[InlineKeyboardButton('Previous', callback_data='help_2'),
35-
InlineKeyboardButton('Next', callback_data='tip_1')]]
41+
[
42+
InlineKeyboardButton("Previous", callback_data="help_2"),
43+
InlineKeyboardButton("Tip", callback_data="tip_1"),
44+
]
45+
]
3646
await query.message.edit(helptext3, reply_markup=InlineKeyboardMarkup(buttons))
3747
await query.answer()
3848

3949

4050
@psm.on_callback_query(dynamic_data_filter("tip_1"))
4151
async def tip_button1(_, query):
42-
buttons = [[
43-
InlineKeyboardButton('Previous', callback_data='help_3'),
44-
InlineKeyboardButton('Source', url='https://github.com/pokurt/Pyrogram-SessionMaker-Bot')
45-
]]
46-
await query.message.edit(tiptext1, reply_markup=InlineKeyboardMarkup(buttons), disable_web_page_preview=True)
52+
buttons = [
53+
[
54+
InlineKeyboardButton("Previous", callback_data="help_3"),
55+
InlineKeyboardButton(
56+
"Source", url="https://github.com/pokurt/Pyrogram-SessionMaker-Bot"
57+
),
58+
]
59+
]
60+
await query.message.edit(
61+
tiptext1,
62+
reply_markup=InlineKeyboardMarkup(buttons),
63+
disable_web_page_preview=True,
64+
)
4765
await query.answer()

0 commit comments

Comments
 (0)