如何在电报中制作动态命令
How to make Dynamic commands in telegram
如何在不为每个 ID 创建方法的情况下为数据库中的项目发送 ID
我的目标是输入所需的 ID /13
和机器人 returns 我需要的有关该项目的信息
选择的回购是pyTelegramBotAPI
对于数据库访问和操作,我使用的是 flask-sqlalchemy
import telebot
from config import telegram_token
bot = telebot.TeleBot(telegram_token)
@bot.message_handler(commands=['summary'])
def send_welcome(message):
summary = """ return general info about bot performance """
bot.reply_to(message, summary )
@bot.message_handler(commands=['<id>'])
def send_welcome(message):
id_ = """ return info about this id"""
bot.reply_to(message, id_)
您的 link 到 API 显示了使用 regex
识别命令的方法
@bot.message_handler(regexp="SOME_REGEXP")
您可以将其与 regexp="/\d+"
一起使用以获得任何 /number
。
稍后您应该使用 message.text[1:]
将此 number
获取为字符串(不带 /
)。
@bot.message_handler(regexp="/\d+")
def get_id(message):
id_ = message.text[1:] # as string without `/`
#id_ = int(message.text[1:]) # as integer
bot.reply_to(message, id_)
编辑:
为了运行 功能只为/number
它还需要^ $
regexp="^/\d+$"
如果没有 ^ $
,它将 运行 也适用于 hello /13 world
最少的工作代码
import os
import telebot
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
bot = telebot.TeleBot(TELEGRAM_TOKEN)
@bot.message_handler(regexp="^\d+$")
def get_id(message):
id_ = message.text[1:] # as string without `/`
#id_ = int(message.text[1:]) # as integer
bot.reply_to(message, id_)
bot.polling()
编辑:
您也可以使用
@bot.message_handler(func=lambda message: ...)
喜欢
@bot.message_handler(func=lambda msg: msg.text[0] == '/' and msg.text[1:].isdecimal())
def get_id(message):
id_ = message.text[1:] # as string without `/`
#id_ = int(message.text[1:]) # as integer
bot.reply_to(message, id_)
或更具可读性
def is_number_command(message):
text = message.text
return text[0] == '/' and text[1:].isdecimal()
@bot.message_handler(func=is_number_command)
def get_id(message):
id_ = message.text[1:] # as string without `/`
#id_ = int(message.text[1:]) # as integer
bot.reply_to(message, id_)
您可以使用text[1:].isdecimal()
或text[1:].isdigit()
或text[1:].isnumeric()
或str.isdecimal(text[1:])
或str.isdigit(text[1:])
或str.isnumeric(text[1:])
有了 func
,您可以使用其他功能使其变得更加复杂。
如何在不为每个 ID 创建方法的情况下为数据库中的项目发送 ID
我的目标是输入所需的 ID /13
和机器人 returns 我需要的有关该项目的信息
选择的回购是pyTelegramBotAPI
对于数据库访问和操作,我使用的是 flask-sqlalchemy
import telebot
from config import telegram_token
bot = telebot.TeleBot(telegram_token)
@bot.message_handler(commands=['summary'])
def send_welcome(message):
summary = """ return general info about bot performance """
bot.reply_to(message, summary )
@bot.message_handler(commands=['<id>'])
def send_welcome(message):
id_ = """ return info about this id"""
bot.reply_to(message, id_)
您的 link 到 API 显示了使用 regex
识别命令的方法
@bot.message_handler(regexp="SOME_REGEXP")
您可以将其与 regexp="/\d+"
一起使用以获得任何 /number
。
稍后您应该使用 message.text[1:]
将此 number
获取为字符串(不带 /
)。
@bot.message_handler(regexp="/\d+")
def get_id(message):
id_ = message.text[1:] # as string without `/`
#id_ = int(message.text[1:]) # as integer
bot.reply_to(message, id_)
编辑:
为了运行 功能只为/number
它还需要^ $
regexp="^/\d+$"
如果没有 ^ $
,它将 运行 也适用于 hello /13 world
最少的工作代码
import os
import telebot
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
bot = telebot.TeleBot(TELEGRAM_TOKEN)
@bot.message_handler(regexp="^\d+$")
def get_id(message):
id_ = message.text[1:] # as string without `/`
#id_ = int(message.text[1:]) # as integer
bot.reply_to(message, id_)
bot.polling()
编辑:
您也可以使用
@bot.message_handler(func=lambda message: ...)
喜欢
@bot.message_handler(func=lambda msg: msg.text[0] == '/' and msg.text[1:].isdecimal())
def get_id(message):
id_ = message.text[1:] # as string without `/`
#id_ = int(message.text[1:]) # as integer
bot.reply_to(message, id_)
或更具可读性
def is_number_command(message):
text = message.text
return text[0] == '/' and text[1:].isdecimal()
@bot.message_handler(func=is_number_command)
def get_id(message):
id_ = message.text[1:] # as string without `/`
#id_ = int(message.text[1:]) # as integer
bot.reply_to(message, id_)
您可以使用text[1:].isdecimal()
或text[1:].isdigit()
或text[1:].isnumeric()
或str.isdecimal(text[1:])
或str.isdigit(text[1:])
或str.isnumeric(text[1:])
有了 func
,您可以使用其他功能使其变得更加复杂。