-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.py
70 lines (63 loc) · 2.22 KB
/
send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from telegram.client import Telegram
import os
import pickle
import time
tg = Telegram(
api_id='your_api_id',
api_hash='your_hash',
phone='your_phone',
library_path='path_to_tdjson',
database_encryption_key='any_key',
files_directory='tmp'
)
tg.login()
result = tg.get_me()
result.wait()
chat_id = result.update['id'] # seems like it also serves as saved messages chat id
orig_converted_msgs = pickle.load(open('orig_converted_msgs.pickle', 'rb'))
sleep_time = 1 # in seconds as a precaution against flood prevention delays
def check_request(request):
request.wait()
if request.error:
print(request.error_info)
time.sleep(2000)
if 'sending_state' in request.update:
if request.update['sending_state'] == 'messageSendingStatePending':
print(msg_i, 'messageSendingStatePending')
time.sleep(100)
elif request.update['sending_state'] == 'messageSendingStateFailed':
print(msg_i, 'messageSendingStateFailed')
time.sleep(2000) # precautions against flood_wait
time.sleep(sleep_time)
requests = []
for msg_i, (orig, converted) in enumerate(orig_converted_msgs[:n]):
if len(converted['text']) > 20: # 20 means there is only date and newline
request = tg.send_message(chat_id, converted['text'])
requests.append(request)
check_request(request)
if 'file' in converted:
data = {
'@type': 'sendMessage',
'chat_id': chat_id,
'input_message_content': {
'@type': 'inputMessageDocument',
'document': {'@type': 'inputFileLocal',
'path': converted['file']},
},
}
request = tg._send_data(data)
requests.append(request)
check_request(request)
if 'photo' in converted:
data = {
'@type': 'sendMessage',
'chat_id': chat_id,
'input_message_content': {
'@type': 'inputMessagePhoto',
'photo': {'@type': 'inputFileLocal',
'path': converted['photo']},
},
}
request = tg._send_data(data)
requests.append(request)
check_request(request)