Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试将变量作为命令放置_Python_Variables_Telegram Bot_Python Telegram Bot_Telepot - Fatal编程技术网

Python 尝试将变量作为命令放置

Python 尝试将变量作为命令放置,python,variables,telegram-bot,python-telegram-bot,telepot,Python,Variables,Telegram Bot,Python Telegram Bot,Telepot,我的变量URL从消息中查找URL。如果我的机器人从收到的消息中找到URL,我希望它发送yes。这是我试过的 def action(msg): chat_id = msg['chat']['id'] command = msg['text'] urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', command)

我的变量
URL
从消息中查找URL。如果我的机器人从收到的消息中找到URL,我希望它发送
yes
。这是我试过的

def action(msg):
    chat_id = msg['chat']['id']
    command = msg['text']

    urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', command)

    if command == urls:
        telegram_bot.sendMessage(chat_id, "yes", parse_mode= 'Markdown')

但它不起作用。将变量作为命令以及如何修复它是一种正确的方法吗?

问题似乎在于您将
命令(字符串)与
URL(字符串列表)进行比较。如果希望在命令中至少找到一个URL时发送消息,可以将其更改为

def操作(msg):
chat_id=msg['chat']['id']
command=msg['text']
URL=re.findall('http[s]?:/(?:[a-zA-Z]|[0-9]|[$-|@和+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])+,命令)
如果URL为:
telegram\u bot.sendMessage(聊天室id,“是”,解析模式='Markdown')
注意-如果没有匹配项,
URL
将是一个空列表。在Python中,空列表的布尔值为false,因此
if-urls
仅在
urls
不是空列表时传递(即至少有一个匹配项)。这相当于说
if len(url)!=0:

如果您希望仅当
命令的全部内容是URL时才发送消息,您可以这样做

def操作(msg):
chat_id=msg['chat']['id']
command=msg['text']
模式='http[s]?:/(?:[a-zA-Z]|[0-9]|[$-|&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])+'
如果重新匹配(模式、命令):
telegram\u bot.sendMessage(聊天室id,“是”,解析模式='Markdown')

“它不工作”-什么不工作?你预期会发生什么?实际发生了什么?什么是
命令
?一根绳子
URL
是一个字符串列表,因此可能存在问题。您是否检查了URL和命令的类型?@Nathan如果我的机器人从收到的消息中找到URL,我希望它回复
yes
。但我试过的东西不起作用。