-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramBot.py
More file actions
202 lines (179 loc) · 6.19 KB
/
TelegramBot.py
File metadata and controls
202 lines (179 loc) · 6.19 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
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Bot to send timed Telegram messages.
Press Ctrl-C on the command line or send a signal to the process to stop the
bot.
"""
import os
import pyspeedtest
import random
import configparser
import subprocess
import urllib.request
import requests
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
def start(bot, update, job_queue, chat_data):
update.message.reply_text('Hi! Started!')
chat_id = update.message.chat_id
if 'job' in chat_data:
update.message.reply_text('you already have an active timer')
return
try:
chat_data['job']
return
except Exception:
job_queue.run_once(alarm, 86400, context=chat_id)
sendStatus(bot, chat_id)
def alarm(bot, job):
"""Send the alarm message."""
sendStatus(bot, job.context)
def getimage(bot, update):
configParser = configparser.RawConfigParser()
configFilePath = r'TelegramBot.config'
configParser.read(configFilePath)
if __debug__:
print("trafficimage")
chat_id = update.message.chat_id
if __debug__:
print(chat_id)
addresses = configParser.get('BOTCONFIG', 'urls').split(',')
if __debug__:
print(addresses)
imagename = 'getImg.jpg'
for address in addresses:
if __debug__:
print(address)
urllib.request.urlretrieve(address, imagename)
if __debug__:
print("requestDone")
bot.send_photo(chat_id=chat_id, photo=open(imagename, 'rb'))
if __debug__:
print("photo sended")
os.remove(imagename)
def gethighwayvid(bot, update):
if __debug__:
print("autostrada")
configParser = configparser.RawConfigParser()
configFilePath = r'TelegramBot.config'
configParser.read(configFilePath)
addresses = configParser.get('BOTCONFIG', 'urlsHighway').split(',')
if __debug__:
print(addresses)
name = "666Devil.mp4"
if __debug__:
print("prima di ciclo adress")
for address in addresses:
if __debug__:
print(address)
r = requests.get(address)
f = open(name, 'wb')
for chunk in r.iter_content(chunk_size=255):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.close()
bot.send_video(chat_id=update.message.chat_id,
video=open(name, 'rb'), supports_streaming=True)
os.remove(name)
def getstatus(bot, update):
chat_id = update.message.chat_id
sendStatus(bot, chat_id)
def makecoffe(bot, update):
configParser = configparser.RawConfigParser()
configFilePath = r'TelegramBot.config'
configParser.read(configFilePath)
if __debug__:
print("config parser readed")
chat_id = update.message.chat_id
insults = configParser.get('BOTCONFIG', 'insults').split(',')
rand = random.randint(0, len(insults) - 1)
user = update.message.from_user
name = user.first_name
surname = user.last_name
completiinsult = "scolta {} {}, {}".format(name, surname, insults[rand])
bot.send_message(chat_id, completiinsult)
def sendStatus(bot, chat_id):
if __debug__:
print("sendstatus")
if __debug__:
print('before sending')
ip = subprocess.check_output(["hostname", "-I"]).decode('utf-8')
if __debug__:
print(ip)
temp = os.popen("vcgencmd measure_temp").readline()
if __debug__:
print(temp)
uptime = subprocess.check_output(['uptime']).decode('utf-8')
if __debug__:
print(uptime)
bot.send_message(chat_id, 'Ip={}{}Uptime={}'.format(ip, temp, uptime))
def networkstats(bot, update):
chat_id = update.message.chat_id
configParser = configparser.RawConfigParser()
configFilePath = r'TelegramBot.config'
configParser.read(configFilePath)
st = pyspeedtest.SpeedTest(configParser.get('BOTCONFIG', 'speedtestUrl'))
if __debug__:
print('speedtest initialized')
try:
ping = "{0:.2f}".format(st.ping())
if __debug__:
print(ping)
except Exception:
ping = "error on ping"
if __debug__:
print("error ping")
try:
download = "{0:.2f}".format(st.download())
if __debug__:
print(download)
except Exception:
download = "error on download"
if __debug__:
print("error download")
try:
upload = "{0:.2f}".format(st.upload())
if __debug__:
print(upload)
except Exception:
upload = "error on upload"
if __debug__:
print("error upload")
bot.send_message(chat_id, "Ping={}DW={}UP={}".format(
ping, download, upload))
def unset(bot, update, chat_data):
"""Remove the job if the user changed their mind."""
if 'job' not in chat_data:
update.message.reply_text('You have no active timer')
return
job = chat_data['job']
job.schedule_removal()
del chat_data['job']
update.message.reply_text('Timer successfully unset!')
def main():
"""Run bot."""
configParser = configparser.RawConfigParser()
configFilePath = r'TelegramBot.config'
configParser.read(configFilePath)
if __debug__:
print("config parser readed")
updater = Updater(configParser.get('BOTCONFIG', 'botId'))
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler(
"start", start, pass_job_queue=True, pass_chat_data=True))
dp.add_handler(CommandHandler("help", start))
dp.add_handler(CommandHandler("getstatus", getstatus))
dp.add_handler(CommandHandler("networkstats", networkstats))
dp.add_handler(CommandHandler("makecoffe", makecoffe))
dp.add_handler(CommandHandler("getimage", getimage))
dp.add_handler(CommandHandler("gethighwayvid", gethighwayvid))
dp.add_handler(CommandHandler("unset", unset, pass_chat_data=True))
# Start the Bot
updater.start_polling(5)
# Block until you press Ctrl-C or the process receives SIGINT, SIGTERM or
# SIGABRT. This should be used most of the time, since start_polling() is
# non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()