Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sending messages using Python Selenium for Telegram Bot

I’m trying to automate sending messages on Telegram using Selenium and a Python library. However, I keep encountering an error. 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()

I’m getting the following error: AttributeError: ‘TeleBot’ object has no attribute ‘send_message’. How can I fix this?

Tried other libraries, but im new at this and I don’t understand it correctly

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading