|
1 | 1 | 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 |
4 | 4 |
|
5 | 5 |
|
6 | 6 | configur = ConfigParser()
|
|
12 | 12 | api_id = configur.get('app_config', 'api_id')
|
13 | 13 | api_hash = configur.get('app_config', 'api_hash')
|
14 | 14 | token = configur.get('bot_api', 'token')
|
15 |
| - |
16 |
| -# your phone number |
| 15 | + |
| 16 | +# your phone number |
17 | 17 | 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 | + |
36 | 22 | # receiver user_id and access_hash
|
37 | 23 | user_id = configur.get('receiver_details', 'user_id')
|
38 | 24 | user_hash = configur.get('receiver_details', 'user_hash')
|
39 | 25 |
|
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