-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeleClient.py
More file actions
190 lines (158 loc) · 7.38 KB
/
Copy pathTeleClient.py
File metadata and controls
190 lines (158 loc) · 7.38 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import time
from datetime import datetime, timedelta
from telethon import TelegramClient, events
import os.path
# Use your own values from my.telegram.org
api_id = 1168521
api_hash = ''
client = TelegramClient('anon', api_id, api_hash)
MyChat = -487653262
logFilePath = "pollutionLog.txt"
PollCycleTime_min = 30
small_delay = 3
# Pollution logger variables
districtListRequest = [
"Поділ", "Троєщина", "Видубичі", "Печерськ",
"Університет", "Протасів Яр", "Липки", "Оболонь",
"Дарниця", "Харківський мас.", "Лісовий мас.", "Соцмісто",
"Осокорки", "Нивки", "Шулявка", "Відрадний",
"Голосіїв", "Лук’янівка", "Позняки", "Борщагівка",
"Солом’янка", "Святошин", "Воскресенка", "Русанівка",
"Мінський мас.","Теремки", "м. Вишневе", "м. Ірпінь",
"м. Бориспіль", "м. Бровари", "ВДНГ"
]
districtListResponse = [
"Поділ", "Троєщина", "Видубичі", "Печерськ",
"Університет", "Протасів Яр", "Липки", "Оболонь",
"Дарниця", "Харківський мас.", "Лісовий мас.", "Соцмісто",
"Осокорки", "Нивки", "Шулявка", "Відрадний",
"Голосіїв", "Лук’янівка", "Позняки", "Борщагівка",
"Солом’янка", "Святошин", "Воскресенка", "Русанівка",
"Мінський мас.","Теремки", "Вишневе", "Ірпінь",
"Бориспіль", "Бровари", "ВДНГ"
]
districtIterator = 0
logStr = ""
# Telethon update handler for new messages
@client.on(events.NewMessage(chats=MyChat))
async def normal_handler(event):
await parseNewMessage(event)
# Start aquality bot
async def startBot():
await client.send_message(MyChat, '/start@aqualitybot')
time.sleep(small_delay)
async def parseNewMessage(event):
global districtIterator
global logStr
global logFile
#print(event.message.text)
# Parse response with:
# Main menu
if "Що цікавить" in event.message.text:
time.sleep(small_delay)
await client.send_message(MyChat, '\U0001F504 Змінити район', reply_to=event.message.id)
# Select district menu
elif "Вибери район" in event.message.text:
time.sleep(small_delay)
await client.send_message(MyChat, districtListRequest[districtIterator], reply_to=event.message.id)
# Fill dummy info to log string to replace it when answer will come
logStr += 'Err\t'
# Answer with pollution level for selected district
elif districtListResponse[districtIterator] in event.message.text:
# Parse pollution level value
pollutionLevel = [int(i) for i in event.message.text.split() if i.isdigit()]
dummyInfoIndex = logStr.rfind("Err")
logStr = logStr[:dummyInfoIndex] + str(pollutionLevel[0]) + '\t'
# Proceed to next district (increment iterator)
districtIterator += 1
# We complited the whole list of districts
if districtIterator >= int(len(districtListResponse)):
# Reset district iterator
districtIterator = 0
# Write log
logFile.write(logStr + '\n')
print(logStr)
logFile.close()
# Count delay to wake up for next poll
currTime = datetime.now()
wakeUpTime = ceil_time(currTime, timedelta(minutes=PollCycleTime_min))
#print(str((wakeUpTime-currTime).seconds))
# Write time stamp for next log entry
logStr = str(wakeUpTime) + '\t'
# Sleep until next poll
time.sleep((wakeUpTime-currTime).seconds)
logFile = open(logFilePath, "a+", encoding='utf-8')
def ceil_time(currTime, timeDelta):
return currTime + (datetime.min - currTime) % timeDelta
#Prepaire log-file
if os.path.isfile(logFilePath) == True:
# File exists, just open it
logFile = open(logFilePath, "a+", encoding='utf-8')
else:
# There is no log file yet
#Create one and fill header
logFile = open(logFilePath, "a+", encoding='utf-8')
logFile.write('Timestamp\t')
for district in districtListResponse:
logFile.write(district +'\t')
logFile.write('\n')
logStr = str(datetime.now())+'\t'
# Start client
client.start()
# Start aqualityBot
with client:
client.loop.run_forever()
#Run untill complition (forever?)
#client.run_until_disconnected()
"""
for i in range(0):
# Get last message:
async for message in client.iter_messages('test', 1):
pass
# Select Change district menu
await client.send_message(-487653262, '\U0001F504 Змінити район', reply_to=message.id)
time.sleep(small_delay)
# Get last message:
async for message in client.iter_messages('test', 1):
pass
# Get timestamp for all measurements
timestamp = message.date
logFile.write('\n'+str(timestamp)+'\t')
# For loop through all districts
for district in districtList:
# Select district
await client.send_message(-487653262, district, reply_to=message.id)
time.sleep(small_delay)
# Get previous to last message (dirty-dirty stuff)
i = 0;
async for message in client.iter_messages('test', 2):
if i == 0:
i += 1
continue
parsedDistrict = message.text.split(':')[0]
pollutionLevel = [int(i) for i in message.text.split() if i.isdigit()]
if parsedDistrict == district:
logFile.write(str(pollutionLevel[0])+'\t')
#print(str(timestamp)+'\t- '+district+'\t- '+str(pollutionLevel[0]))
else:
logFile.write('Error\t')
print(str(timestamp)+'\t- '+district+'\t- Error')
# Select Change district menu
await client.send_message(-487653262, '\U0001F504 Змінити район', reply_to=message.id+1)
time.sleep(small_delay)
# Get last message:
async for message in client.iter_messages('test', 1):
pass
time.sleep(600)
"""
# Get last message:
# async for message in client.iter_messages('test', 1):
# pass
# Select district
#await client.send_message(-487653262, 'Видубичі', reply_to=message.id)
# print all chats name
#async for dialog in client.iter_dialogs():
# print(dialog.title)
# You can print the message history of any chat:
# async for message in client.iter_messages('test'):
# print(message.id, message.text)