Search code examples
pythonselenium-webdrivertelegram

Can't use the python.h file; Python embedding error


I'm trying to automate sending messages on Telegram using Selenium and TeleBot library. However, I keep encountering the following error:

AttributeError: 'TeleBot' object has no attribute 'send_message'. 

How can I fix this? Here's my code:

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()


Solution

  • You didn't understand the use of TeleBot class from pyTelegramBotAPI. It seems you are mixing Selenium automation with the bot's functionality to send a message. You should either send messages through pyTelegramBotApi or automate user interaction on the web interface using Selenium. You shouldn't use both simultaneously for sending a single message:

    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!")