Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import subprocess;
- import ipaddress;
- import telebot;
- bot = telebot.TeleBot('авотхуйтокен');
- def is_ip(string):
- try:
- ipaddress.ip_address(string)
- return True
- except ValueError:
- return False
- def ping(host):
- command = ['ping', '-c1', '-W1', host]
- return subprocess.call(command) == 0
- @bot.message_handler(commands=['start'])
- def command_start(message):
- bot.send_message(message.from_user.id, 'Привет. Твой ID ' + str(message.from_user.id))
- @bot.message_handler(commands=['id'])
- def command_id(message):
- bot.send_message(message.from_user.id, 'Твой ID ' + str(message.from_user.id))
- @bot.message_handler(commands=['help'])
- def command_help(message):
- bot.send_message(message.from_user.id, "/help - справка\n/id - узнать свой ID\n/ping адрес - пропинговать адрес")
- @bot.message_handler(commands=['ping'])
- def command_ping(message):
- command = message.text.split()
- if is_ip(command[1]):
- proc = subprocess.Popen(['ping', '-c3', '-W1', command[1]], stdout=subprocess.PIPE)
- stdout, stderr = proc.communicate()
- print(stdout.decode('ASCII'))
- bot.send_message(message.from_user.id, stdout.decode('ASCII'))
- else:
- bot.send_message(message.from_user.id, 'Неправильный IP-адрес')
- @bot.message_handler(content_types=['text'])
- def get_text_messages(message):
- bot.send_message(message.from_user.id, 'Неизвестная команда. См /help')
- print(' - - - ')
- print(message.from_user.username + ':')
- print(message.text)
- @bot.callback_query_handler(func=lambda call: True)
- def query_handler(call):
- command = call.data.split()
- if command[0] == 'graph' and is_ip(command[1]):
- bot.answer_callback_query(callback_query_id=call.id, text='График отправляется')
- bot.send_chat_action(call.message.chat.id, 'upload_photo')
- graph = open('/var/www/localhost/htdocs/var/ping/' + command[1] + '.png', 'rb')
- bot.send_photo(call.message.chat.id, graph)
- graph.close()
- elif command[0] == 'ping' and is_ip(command[1]):
- if ping(command[1]):
- bot.answer_callback_query(callback_query_id=call.id, text='Хост отвечает')
- else:
- bot.answer_callback_query(callback_query_id=call.id, text='Хост не отвечает')
- else:
- bot.answer_callback_query(callback_query_id=call.id, text='Ошибка, неправильная команда')
- #bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id)
- bot.polling(none_stop=True, interval=6)
RAW Paste Data