[英]Telegram Bot respond to specific command in Python list
I am making a Telegram bot that can can access database to reply users' query. 我正在制作一个可以访问数据库以回复用户查询的Telegram机器人。 The bot need to respond to specific request of certain data in database.
机器人需要响应数据库中某些数据的特定请求。 I was able to solve for when users request for all data but I am stuck with individual data.
当用户请求所有数据时,我能够解决问题,但是我只能使用单个数据。 I am using
telegram.ext
from telegram
package in python. 我使用
telegram.ext
从telegram
在Python包。 Here is what I have done so far. 到目前为止,这是我所做的。
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import MySQLdb
currr = [] # global list var ~don't bash me for using global in python please, I'm a newbie
# request for all data in database
def request2(bot, update):
db = MySQLdb.connect(host = "local", user = "root", passwd = "pwd", db = "mydb")
cur = db.cursor()
cur.execute("select ID from table")
ID = cur.fetchall()
cur.execute("SELECT ID, temp FROM table2 order by indexs desc")
each_rows = cur.fetchall()
for IDs in ID:
for each_row in each_rows:
if str(each_row[0])[0:4]==str(ID)[2:6]:
update.message.reply_text('reply all related data here')
break
# request for single data
def individualreq(bot, update):
db = pymysql.connect(host = "localhost", user = "root", passwd = "pwd", db = "mydb")
update.message.reply_text('reply individual data to users here')
def main():
updater = Updater("TOKEN")
dp = updater.dispatcher
global currr
# get all ID form database
db = MySQLdb.connect(host = "localhost", user = "root", passwd = "pwd", db = "mydb")
cur = db.cursor()
cur.execute("select ID from table")
curr_ID = cur.fetchall()
# example ID = 'F01', 'F02', 'F03'
for curr_IDs in curr_ID:
currr.append(curr_IDs[0])
# request all data
dp.add_handler(CommandHandler("all", request2))
# request individual data
dp.add_handler(CommandHandler(currr, individualreq)) # list command in currr[]
if __name__ == '__main__':
main()
I am looking for a way to pass the current command which is also the ID in database that user request in the currr[]
list to the individualreq(bot, update)
function so that only data of the called ID is being replied. 我正在寻找一种方法,将当前命令(也是用户在
currr[]
列表中请求的数据库中的ID)传递给currr[]
individualreq(bot, update)
函数,以便仅对被调用ID的数据进行复制。 Users will select from a list of ID in telegram and the command handler can pass the selected ID to the function. 用户将从电报中的ID列表中进行选择,命令处理程序可以将所选的ID传递给函数。 I have not found a way to pass the ID to the function.
我还没有找到将ID传递给函数的方法。 Could someone help me to solve this please.
有人可以帮我解决这个问题。 Thanks
谢谢
I find out a solution for my question from the answer provided by Oluwafemi Sule. 我从Oluwafemi Sule提供的答案中找到了我的问题的解决方案。
CommandHandler
can pass the arguments of the command to the function by adding pass_args=True
in the CommandHandler
. CommandHandler
可以通过添加传递命令到函数的参数pass_args=True
在CommandHandler
。
dp.add_handler(CommandHandler(currr, individualreq, pass_args=True))
To print out the args in the function, the function need to receive the args. 要打印出函数中的参数,该函数需要接收参数。
def individualreq(bot, update, args):
# id store the args value
id = update.message.text
print(id[1:]) # [1:] is to get rid of the / in id
You can outright make individualreq
a closure.您可以直接将 individualreq
封闭。
CommandHandler
takes a command or list of command to listen to and a list other options. CommandHandler
接受一个命令或命令列表以进行侦听,并列出其他选项。 There is a pass_user_data
option that allows for user data to be passed to the callback. 有一个
pass_user_data
选项 ,该选项允许将用户数据传递给回调。
dp.add_handler(CommandHandler(currr, individualreq, pass_user_data=True))
The signature for individualreq
callback will be updated to take the user_data
对于签名
individualreq
回调将被更新采取user_data
def individualreq(bot, update, user_data=None):
#user_data is a dict
print(user_data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.