Skip to content

Commit 2a0e241

Browse files
committed
Code modularization of telegram script
Signed-off-by: Ameya Deshpande <ameyanrd@outlook.com>
1 parent b8c17f7 commit 2a0e241

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

Scripts/Bots/session.session

28 KB
Binary file not shown.

Scripts/Bots/telegram_bot.py

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from configparser import ConfigParser
2-
from telethon.sync import TelegramClient
3-
from telethon.tl.types import InputPeerUser
2+
from telethon.sync import TelegramClient
3+
from telethon.tl.types import InputPeerUser
44

55

66
configur = ConfigParser()
@@ -12,42 +12,57 @@
1212
api_id = configur.get('app_config', 'api_id')
1313
api_hash = configur.get('app_config', 'api_hash')
1414
token = configur.get('bot_api', 'token')
15-
16-
# your phone number
15+
16+
# your phone number
1717
phone = configur.get('client_details', 'phone')
18-
19-
# creating a telegram session and assigning
20-
# it to a variable client
21-
client = TelegramClient('session', api_id, api_hash)
22-
23-
# connecting and building the session
24-
client.connect()
25-
26-
# in case of script ran first time it will
27-
# ask either to input token or otp sent to
28-
# number or sent or your telegram id
29-
if not client.is_user_authorized():
30-
31-
client.send_code_request(phone)
32-
33-
# signing in the client
34-
client.sign_in(phone, input('Enter the code: '))
35-
18+
19+
# client variable
20+
client = None
21+
3622
# receiver user_id and access_hash
3723
user_id = configur.get('receiver_details', 'user_id')
3824
user_hash = configur.get('receiver_details', 'user_hash')
3925

40-
try:
41-
42-
receiver = InputPeerUser(user_id, user_hash)
43-
44-
# sending message using telegram client
45-
client.send_message(receiver, "Hello", parse_mode='html')
46-
except Exception as e:
47-
48-
# there may be many error coming in while like peer
49-
# error, wwrong access_hash, flood_error, etc
50-
print(e);
51-
52-
# disconnecting the telegram session
53-
client.disconnect()
26+
def client_connect(client):
27+
# creating a telegram session and assigning
28+
# it to a variable client
29+
client = TelegramClient('session', api_id, api_hash)
30+
31+
# connecting and building the session
32+
client.connect()
33+
34+
def client_authenticate(client, phone):
35+
# in case of script ran first time it will
36+
# ask either to input token or otp sent to
37+
# number or sent or your telegram id
38+
if not client.is_user_authorized():
39+
40+
client.send_code_request(phone)
41+
42+
# signing in the client
43+
client.sign_in(phone, input('Enter the code: '))
44+
45+
46+
def messasge_send(client, user_id, user_hash):
47+
try:
48+
49+
receiver = InputPeerUser(user_id, user_hash)
50+
51+
# sending message using telegram client
52+
client.send_message(receiver, "Hello", parse_mode='html')
53+
except Exception as e:
54+
55+
# there may be many error coming in while like peer
56+
# error, wwrong access_hash, flood_error, etc
57+
print(e);
58+
59+
def client_disconnect(client):
60+
# disconnecting the telegram session
61+
client.disconnect();
62+
63+
64+
client_connect(client)
65+
66+
client_authenticate(client, phone)
67+
68+
client_disconnect(client)

0 commit comments

Comments
 (0)