-
Notifications
You must be signed in to change notification settings - Fork 11
/
run.py
97 lines (88 loc) · 4.35 KB
/
run.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
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
from dotenv import load_dotenv
import os
from telebot.async_telebot import AsyncTeleBot
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
import telebot
import asyncio
from search import *
load_dotenv()
ZHELPER_BOT_TOKEN = os.environ['ZHELPER_BOT_TOKEN']
LISTEN = os.environ['LISTEN']
PORT = os.environ['PORT']
WEBHOOK_SSL_CERT = os.environ['WEBHOOK_SSL_CERT']
WEBHOOK_SSL_PRIV = os.environ['WEBHOOK_SSL_PRIV']
bot = AsyncTeleBot(ZHELPER_BOT_TOKEN)
async def extract_unique_code(text):
return text.split()[1] if len(text.split()) > 1 else None
@bot.message_handler(commands=['start'])
async def send_welcome(message):
unique_code = await extract_unique_code(message.text)
if unique_code:
reply_content=await detail(unique_code)
try:
await(bot.reply_to(message, reply_content, parse_mode="Markdown"))
except:
await(bot.reply_to(message, 'Connection Error, please contact bot admin\n连接错误,请联系机器人管理员'))
else:
reply = """Welcome to Zhelper bot! Please type your book name and we will find the book for you.
zhelper international group: https://t.me/zhelperorg
欢迎使用zhelper机器人!请直接输入书名进行搜索。zhelper官方中文群https://t.me/zhelpert"""
await bot.reply_to(message, reply)
@bot.message_handler(commands=['help'])
async def send_welcome(message):
await(bot.reply_to(message, """Welcome to Zhelper bot! Please type your book name and we will find the book for you.
zhelper international group: https://t.me/zhelperorg
欢迎使用zhelper机器人!请直接输入书名进行搜索。zhelper官方中文群https://t.me/zhelpert"""))
async def gen_markup(t):
markup = InlineKeyboardMarkup()
markup.row_width = 2
markup.add(InlineKeyboardButton("(local database) 离线数据库", callback_data="v5."+t+".1"))
markup.add(InlineKeyboardButton("(online database)在线数据库", callback_data="v4."+t+".1"))
return markup
async def gen_page_markup(method,t,page):
markup = InlineKeyboardMarkup()
if page!="1":
markup.add(InlineKeyboardButton("<-", callback_data="v5."+t+"."+str(int(page)-1)),
InlineKeyboardButton(page,callback_data="ignore"),
InlineKeyboardButton("->", callback_data=method+t+"."+str(int(page)+1)))
else:
markup.add(InlineKeyboardButton("<-", callback_data="ignore"),
InlineKeyboardButton(page,callback_data="ignore"),
InlineKeyboardButton("->", callback_data=method+t+"."+str(int(page)+1)))
return markup
@bot.callback_query_handler(func=lambda call: True)
async def callback_query(call):
data=call.data
if data=="ignore":
return
command=data.split('.')[0]
content=data.split('.')[1]
page=data.split('.')[2]
if command == "v4":
reply_content=await v4(content,page)
try:
await bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text=reply_content, reply_markup=await gen_page_markup('v4.',content,page),parse_mode='Markdown')
except Exception as e:
print(str(e))
await bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text='Unable to find books. Try using other keywords?\n我们无法找到这本书,请尝试更换关键词')
elif command == "v5":
reply_content=await v5(content,page)
try:
await bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text=reply_content, reply_markup=await gen_page_markup('v5.',content,page),parse_mode='Markdownv2')
except Exception as e:
print(str(e))
await bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id,
text='Unable to find books. Try using other keywords?\n我们无法找到这本书,请尝试更换关键词')
@bot.message_handler(func=lambda message: True)
async def allmessage(message):
await bot.reply_to(message, "Please choose the database\n请选择数据库", reply_markup=await gen_markup(message.text))
# asyncio.run(setcommands())
asyncio.run(bot.run_webhooks(
listen=LISTEN,
port=int(PORT),
certificate=WEBHOOK_SSL_CERT,
certificate_key=WEBHOOK_SSL_PRIV
))