This repository has been archived by the owner on Feb 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
57 lines (44 loc) · 1.73 KB
/
bot.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
from telegram.ext import Updater
from telegram.ext import CommandHandler
from main import query,QueryException
from json.decoder import JSONDecodeError
import logging
import json
import os
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
token = os.environ.get('token')
updater = Updater(token=token)
dispatcher = updater.dispatcher
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id,
text='欢迎使用云悉机器人,请使用/help获取使用方法')
def help(bot, update):
bot.send_message(chat_id=update.message.chat_id,
text='使用/query <domain> 查询一个域名信息'
)
def _query(bot, update, args):
if len(args) != 1:
bot.send_message(chat_id=update.message.chat_id, text='命令格式 /query <domain>')
return
try:
ret = query(args[0])
except QueryException as e:
bot.send_message(chat_id=update.message.chat_id, text='已推送云端检测,请几分钟后再查询')
return
except JSONDecodeError as e:
bot.send_message(chat_id=update.message.chat_id, text='查询错误,请几分钟后再查询')
return
text = ""
for data in ret:
for key,value in data.items():
text = text + "{} ".format(value)
text = text+"\n"
bot.send_message(chat_id=update.message.chat_id, text=text)
caps_handler = CommandHandler('query', _query, pass_args=True)
dispatcher.add_handler(caps_handler)
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
help_handler = CommandHandler('help', help)
dispatcher.add_handler(help_handler)
print("bot start")
updater.start_polling()