You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python开发whois查询机器人调用send_message缺少text参数报错如何解决

报错原因

send_message() 是pyTelegramBotAPI提供的方法,要求第一个必填参数是接收消息的聊天ID(chat_id),第二个必填参数才是需要发送的文本内容。
你当前的写法bot.send_message(infa1)只传入了一个参数,函数会默认把infa1识别为聊天ID,自然就提示缺少必填的text参数,和infa1本身有没有正常存储内容无关。

修复方案

你已经在函数开头拿到了当前触发命令的用户聊天ID,存在uid变量中,只需要修改send_message的传参即可:

# 修正前的错误代码
# bot.send_message(infa1)

# 修正后的代码
bot.send_message(uid, infa1)

额外优化建议

你当前的代码存在get_screenshot函数重复装饰、嵌套定义的问题,会导致逻辑重复执行,同时用完浏览器后没有主动关闭会造成后台进程残留,推荐调整后的完整代码参考:

@bot.message_handler(commands=['getpng'])
def get_screenshot(message):
    uid = message.chat.id
    url = ""
    try:
        url = message.text.split(' ')[1]
    except IndexError:
        bot.send_message(uid, 'You have not entered URL!')
        return
    if not validators.url(url):
        bot.send_message(uid, 'URL is invalid!')
        return
    browser = webdriver.Chrome()
    try:
        browser.get('https://www.nic.ru/whois/')
        input1 = browser.find_element_by_xpath('//*[@id="rc-web-client"]/div/div/div[1]/main/div[1]/div[1]/div/div/div/input')
        input1.send_keys(url)
        time.sleep(2)
        button = browser.find_element_by_xpath('//*[@id="rc-web-client"]/div/div/div[1]/main/div[1]/div[1]/div/button/span[1]')
        button.click()
        time.sleep(5)
        infa = browser.find_element_by_xpath('//*[@id="rc-web-client"]/div/div/div[1]/main/div[1]/div[4]/div/div[2]/a[1]')
        infa1 = infa.text
        bot.send_message(uid, infa1)
    finally:
        browser.quit()

内容的提问来源于stack exchange,提问作者beltz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 20:54:05