2019-06-22 21:07:44 +08:00
|
|
|
#!/usr/bin/env python3.6
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#import logging
|
|
|
|
import config
|
|
|
|
import telebot
|
|
|
|
from telebot import apihelper, types
|
|
|
|
from importer import Importer
|
2019-06-23 11:01:10 +08:00
|
|
|
import os
|
2019-06-22 21:07:44 +08:00
|
|
|
|
2019-06-24 11:20:08 +08:00
|
|
|
if config.proxy:
|
|
|
|
apihelper.proxy = {'https': config.proxy_addr}
|
2019-06-22 21:07:44 +08:00
|
|
|
|
|
|
|
bot = telebot.TeleBot(config.bot_token)
|
|
|
|
|
|
|
|
#logger = telebot.logger
|
|
|
|
#telebot.logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
def check_permission(uid):
|
2019-06-23 14:02:22 +08:00
|
|
|
if uid not in config.users:
|
|
|
|
return False
|
|
|
|
return True
|
2019-06-22 21:07:44 +08:00
|
|
|
|
|
|
|
def file_ext(filename):
|
|
|
|
return filename.split(".")[-1]
|
|
|
|
|
2019-06-24 13:50:39 +08:00
|
|
|
def extract_text(text):
|
|
|
|
try:
|
|
|
|
return text.split()[1]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2019-06-22 21:07:44 +08:00
|
|
|
@bot.message_handler(commands=['help', 'start'])
|
|
|
|
def send_welcome(message):
|
2019-06-28 09:15:29 +08:00
|
|
|
bot.reply_to(message, "Enter me commands")
|
2019-06-22 21:07:44 +08:00
|
|
|
|
2019-06-23 12:43:02 +08:00
|
|
|
@bot.message_handler(commands=['id', 'myid'])
|
|
|
|
def my_id(message):
|
|
|
|
bot.send_message(message.chat.id, 'Твой ID: ' + str(message.chat.id))
|
|
|
|
|
2019-06-24 13:50:39 +08:00
|
|
|
@bot.message_handler(commands=['ping'])
|
|
|
|
def ping(message):
|
|
|
|
ip = extract_text(message.text)
|
|
|
|
if not ip:
|
2019-06-28 09:15:29 +08:00
|
|
|
bot.send_message(message.chat.id, 'Use command /ping 127.0.0.1')
|
2019-06-24 13:50:39 +08:00
|
|
|
else:
|
2019-06-24 15:50:23 +08:00
|
|
|
resource = os.system("ping -c 1 -w 1 " + ip)
|
2019-06-24 13:50:39 +08:00
|
|
|
if resource == 0:
|
|
|
|
bot.reply_to(message, "Is UP")
|
|
|
|
else:
|
|
|
|
bot.reply_to(message, "Is down")
|
|
|
|
|
2019-06-22 21:07:44 +08:00
|
|
|
@bot.message_handler(func=lambda message: True)
|
|
|
|
def echo_message(message):
|
|
|
|
bot.reply_to(message, message.text)
|
|
|
|
|
|
|
|
@bot.message_handler(content_types=['document'])
|
|
|
|
def handle_file(message):
|
|
|
|
try:
|
|
|
|
if check_permission(message.from_user.id):
|
|
|
|
file_info = bot.get_file(message.document.file_id)
|
|
|
|
downloaded_file = bot.download_file(file_info.file_path)
|
|
|
|
|
|
|
|
if file_ext(message.document.file_name) == 'csv':
|
2019-06-23 11:02:06 +08:00
|
|
|
src='/tmp/'+message.document.file_name;
|
2019-06-22 21:07:44 +08:00
|
|
|
with open(src, 'wb') as new_file:
|
|
|
|
new_file.write(downloaded_file)
|
2019-06-28 09:15:29 +08:00
|
|
|
bot.reply_to(message,"File added")
|
2019-06-22 21:07:44 +08:00
|
|
|
imp = Importer(src, config.zabbix_user, config.zabbix_pass, config.zabbix_api)
|
|
|
|
imp.create_host()
|
2019-06-23 11:01:10 +08:00
|
|
|
os.remove(src)
|
2019-06-28 09:15:29 +08:00
|
|
|
bot.reply_to(message,"Done")
|
2019-06-22 21:07:44 +08:00
|
|
|
else:
|
|
|
|
bot.reply_to(message, "What is it?")
|
|
|
|
else:
|
2019-06-23 12:47:57 +08:00
|
|
|
pass
|
2019-06-22 21:07:44 +08:00
|
|
|
except Exception as e:
|
|
|
|
bot.reply_to(message,e )
|
|
|
|
|
2019-06-24 10:02:57 +08:00
|
|
|
if __name__ == '__main__':
|
2019-06-24 07:08:40 +08:00
|
|
|
pid = str(os.getpid())
|
2019-06-24 10:02:57 +08:00
|
|
|
pidf = config.pid
|
|
|
|
try:
|
|
|
|
pidfile = open(pidf, 'w')
|
|
|
|
pidfile.write(pid)
|
|
|
|
pidfile.close()
|
|
|
|
bot.polling(none_stop=True, interval=0, timeout=10)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
finally:
|
2019-06-24 15:50:23 +08:00
|
|
|
os.remove(pidf)
|