mirror of
https://github.com/midoks/mdserver-web.git
synced 2026-10-10 04:59:25 +08:00
Update receive_faq.py
This commit is contained in:
@@ -7,6 +7,9 @@ import json
|
||||
import base64
|
||||
import threading
|
||||
|
||||
sys.path.append(os.getcwd() + "/class/core")
|
||||
import mw
|
||||
|
||||
import telebot
|
||||
from telebot import types
|
||||
from telebot.util import quick_markup
|
||||
@@ -31,76 +34,160 @@ def getReadCmd(cmd, msg):
|
||||
return real_msg
|
||||
|
||||
|
||||
def getFaqKw(cmd):
|
||||
matchObj = re.match(r'寻找【(.*?)】问题如下', cmd, re.M | re.I)
|
||||
data = matchObj.groups()
|
||||
if len(data) > 0:
|
||||
return True, data[0]
|
||||
return False, ''
|
||||
|
||||
|
||||
def searchHttpPage(kw='', p=1, size=1):
|
||||
import urllib
|
||||
kw = kw.strip()
|
||||
kw = urllib.parse.quote_plus(kw)
|
||||
|
||||
api = 'https://bbs.midoks.me/plugin.php?id=external_api&f=bbs_search&q=' + kw + \
|
||||
'&size=' + str(size) + '&p=' + str(p)
|
||||
|
||||
# print('url', api)
|
||||
data = mw.httpGet(api)
|
||||
# print(data)
|
||||
data = json.loads(data)
|
||||
# print(data)
|
||||
if data['code'] > -1:
|
||||
|
||||
alist = data['data']['list']
|
||||
r = []
|
||||
for x in alist:
|
||||
tmp = {}
|
||||
tmp['tid'] = x['tid']
|
||||
tmp['subject'] = x['subject']
|
||||
tmp['url'] = 'https://bbs.midoks.me/forum-' + x['tid'] + '-1.html'
|
||||
r.append(tmp)
|
||||
data['data']['list'] = r
|
||||
return data
|
||||
|
||||
|
||||
def searchFaq(bot, message, cmd_text):
|
||||
# print(search_text)
|
||||
return_msg = "你正在寻找【" + cmd_text + "】答案:\n"
|
||||
return_msg += "此功能还在开发中...请持续关注!\n"
|
||||
bot.reply_to(message, return_msg)
|
||||
return True
|
||||
# cmd_text = 'mw'
|
||||
data = searchHttpPage(cmd_text, 1, 5)
|
||||
if data['code'] == 0 and len(data['data']['list']) > 0:
|
||||
keyboard = []
|
||||
|
||||
dlist = data['data']['list']
|
||||
for x in dlist:
|
||||
keyboard.append([types.InlineKeyboardButton(
|
||||
text=x['subject'], url=x['url'])])
|
||||
|
||||
def searchDebugReply(bot, message, cmd_text):
|
||||
markup = types.ReplyKeyboardMarkup(row_width=2)
|
||||
itembtn1 = types.KeyboardButton('a')
|
||||
itembtn2 = types.KeyboardButton('v')
|
||||
itembtn3 = types.KeyboardButton('d')
|
||||
markup.add(itembtn1, itembtn2, itembtn3)
|
||||
bot.send_message(chat_id, "Choose one letter:", reply_markup=markup)
|
||||
keyboard.append([
|
||||
types.InlineKeyboardButton(
|
||||
text="下一页", callback_data='bbs_next_page_2'),
|
||||
types.InlineKeyboardButton(
|
||||
text="第" + str(data['data']['p']) + "页,共" + str(data['data']['page_num']) + "页", callback_data='bbs_page_total')
|
||||
])
|
||||
|
||||
# or add KeyboardButton one row at a time:
|
||||
markup = types.ReplyKeyboardMarkup()
|
||||
itembtna = types.KeyboardButton('a')
|
||||
itembtnv = types.KeyboardButton('v')
|
||||
itembtnc = types.KeyboardButton('c')
|
||||
itembtnd = types.KeyboardButton('d')
|
||||
itembtne = types.KeyboardButton('e')
|
||||
markup.row(itembtna, itembtnv)
|
||||
markup.row(itembtnc, itembtnd, itembtne)
|
||||
bot.send_message(message.chat.id, "Choose one letter:",
|
||||
reply_markup=markup)
|
||||
keyboard.append([types.InlineKeyboardButton(
|
||||
text="关闭消息", callback_data='bbs_search_close')])
|
||||
|
||||
markup = types.ReplyKeyboardRemove(selective=False)
|
||||
bot.send_message(message.chat.id, message, reply_markup=markup)
|
||||
# bot.reply_to(message, "http://baidu.com")
|
||||
return True
|
||||
# print(keyboard)
|
||||
markup = types.InlineKeyboardMarkup(keyboard)
|
||||
bot.send_message(message.chat.id, "寻找【" +
|
||||
cmd_text.strip() + "】问题如下:", reply_markup=markup)
|
||||
else:
|
||||
keyboard = [
|
||||
[
|
||||
types.InlineKeyboardButton(
|
||||
text="论坛", url='https://bbs.midoks.me'),
|
||||
types.InlineKeyboardButton(
|
||||
text="搜索", url='https://bbs.midoks.me/search.php')
|
||||
],
|
||||
[
|
||||
types.InlineKeyboardButton(
|
||||
text="关闭消息", callback_data='bbs_search_close')
|
||||
]
|
||||
|
||||
]
|
||||
markup = types.InlineKeyboardMarkup(keyboard)
|
||||
bot.send_message(
|
||||
message.chat.id, "未找到合适内容,请在官方论坛[bbs.midoks.me]提问!", reply_markup=markup)
|
||||
|
||||
def searchDebugMsgInline(bot, message, cmd_text):
|
||||
button = {
|
||||
"如何在安装面板?": {"callback_data": "bbs_id_1"},
|
||||
"如何在安装面板2?": {"callback_data": "bbs_id_2"},
|
||||
"下一页": {"callback_data": "bbs_pre"},
|
||||
"上一页": {"url": "google.com"}
|
||||
}
|
||||
|
||||
bot.reply_to(message, "Test",
|
||||
reply_markup=quick_markup(button, row_width=2))
|
||||
# bot.send_message(message.chat.id, "Test",reply_markup=quick_markup(button, row_width=2))
|
||||
return True
|
||||
|
||||
|
||||
def searchDebug(bot, message, cmd_text):
|
||||
searchDebugMsgInline(bot, message, cmd_text)
|
||||
searchFaq(bot, message, cmd_text)
|
||||
return True
|
||||
|
||||
|
||||
def answer_callback_query(bot, call):
|
||||
print(call)
|
||||
bot.send_message(call.message.chat.id, "Test Callback")
|
||||
# bot.answer_callback_query(call.id)
|
||||
|
||||
keyword = call.data
|
||||
|
||||
if keyword == 'bbs_search_close':
|
||||
bot.delete_message(chat_id=call.message.chat.id,
|
||||
message_id=call.message.message_id)
|
||||
return
|
||||
|
||||
is_bbs_page = False
|
||||
p = 1
|
||||
if keyword.startswith('bbs_next_page'):
|
||||
is_bbs_page = True
|
||||
p = keyword.replace('bbs_next_page_', '')
|
||||
|
||||
if keyword.startswith('bbs_pre_page'):
|
||||
is_bbs_page = True
|
||||
p = keyword.replace('bbs_pre_page_', '')
|
||||
|
||||
# print("p", p)
|
||||
if is_bbs_page:
|
||||
is_match, cmd_text = getFaqKw(call.message.text)
|
||||
if not is_match:
|
||||
bot.edit_message_text(
|
||||
chat_id=call.message.chat.id, message_id=call.message.message_id, text="出现错误!")
|
||||
return
|
||||
|
||||
data = searchHttpPage(cmd_text, int(p), 5)
|
||||
|
||||
dlist = data['data']['list']
|
||||
# print(data)
|
||||
keyboard = []
|
||||
for x in dlist:
|
||||
keyboard.append([types.InlineKeyboardButton(
|
||||
text=x['subject'], url=x['url'])])
|
||||
|
||||
page_nav = []
|
||||
if int(data['data']['p']) > 1:
|
||||
page_nav.append(types.InlineKeyboardButton(
|
||||
text="上一页", callback_data='bbs_pre_page_' + str(int(p) - 1)))
|
||||
|
||||
if data['data']['page_num'] != data['data']['p']:
|
||||
page_nav.append(types.InlineKeyboardButton(
|
||||
text="下一页", callback_data='bbs_next_page_' + str(int(p) + 1)))
|
||||
|
||||
page_nav.append(types.InlineKeyboardButton(
|
||||
text="第" + str(data['data']['p']) + "页,共" + str(data['data']['page_num']) + "页", callback_data='bbs_page_total'))
|
||||
|
||||
keyboard.append(page_nav)
|
||||
|
||||
keyboard.append([types.InlineKeyboardButton(
|
||||
text="关闭消息", callback_data='bbs_search_close')])
|
||||
|
||||
markup = types.InlineKeyboardMarkup(keyboard)
|
||||
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
|
||||
text=call.message.text, reply_markup=markup)
|
||||
|
||||
|
||||
def run(bot, message):
|
||||
print(message)
|
||||
text_body = message.text
|
||||
# print(text_body)
|
||||
if isThisCmd('/faq:', text_body):
|
||||
cmd_text = getReadCmd('/faq:', text_body)
|
||||
return searchFaq(bot, message, cmd_text)
|
||||
|
||||
if isThisCmd('/debug', text_body):
|
||||
cmd_text = getReadCmd('/debug', text_body)
|
||||
return searchDebug(bot, message, cmd_text)
|
||||
# if isThisCmd('/debug', text_body):
|
||||
# cmd_text = getReadCmd('/debug', text_body)
|
||||
# return searchDebug(bot, message, cmd_text)
|
||||
|
||||
if text_body.find('?') > -1 or text_body.find('?') > -1:
|
||||
return_msg = "你似乎在寻找【" + text_body + "】答案:\n"
|
||||
@@ -111,5 +198,7 @@ def run(bot, message):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(isThisCmd('/?:', '/?:如何在安装面板'))
|
||||
print(getReadCmd('/?:', '/?:如何在安装面板'))
|
||||
# print(isThisCmd('/?:', '/?:如何在安装面板'))
|
||||
# print(getReadCmd('/?:', '/?:如何在安装面板'))
|
||||
# print(searchHttpPage('mw'))
|
||||
print(getFaqKw('寻找【mw】问题如下:'))
|
||||
|
||||
Reference in New Issue
Block a user