使用 Python Selenium for Telegram Bot 发送消息

问题描述 投票:0回答:1

我正在尝试使用 Selenium 和 Python 库在 Telegram 上自动发送消息。但是,我一直遇到错误。这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from telebot import TeleBot

bot = TeleBot("your_token_here")

driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
driver.get("https://web.telegram.org/")
driver.implicitly_wait(10)

login_button = driver.find_element_by_xpath("//button[text()='Log in']")
login_button.click()

phone_input = driver.find_element_by_name("phone_number")
phone_input.send_keys("your_phone_number")

next_button = driver.find_element_by_xpath("//button[text()='Next']")
next_button.click()

bot.send_message(chat_id="your_chat_id", text="Hello, World!")

driver.quit()

我收到以下错误:AttributeError:“TeleBot”对象没有属性“send_message”。我该如何解决这个问题?

尝试过其他库,但我对此很陌生,并且我不正确理解它

python selenium-webdriver telegram
1个回答
0
投票

你没有理解

TeleBot
中的
pyTelegramBotAPI
类的用法。 看来您正在将 Selenium 自动化与机器人的功能混合起来发送消息。您应该通过
pyTelegramBotApi
发送消息,或者 使用 Selenium 在 Web 界面上自动化用户交互。您不应该同时使用两者来发送一条消息:

from telebot import TeleBot
bot = TeleBot("your_token")
def send_telegram_message(chat_id, message):
    try:
        bot.send_message(chat_id, message)
        print("Message sent.")
    except Exception as e:
        print(f"Failed to send message: {str(e)}")
send_telegram_message("your_chat_id", "Hello, World!")
© www.soinside.com 2019 - 2024. All rights reserved.