My code can't send message to Telegram while running on Replit

I am running same code on Pycharm and it works well. However, when it runs on Replit, it doesn’t send message to my Telegram channel. Here is my send message function:

def telegram_bot_sendtext(
    bot_mesaj, id: str
):  # Telegram bold: * * , italik: _ _
    bot_token = config.telegram_bot_token
    bot_chatID = id
    url = (
        "https://api.telegram.org/bot"
        + bot_token
        + "/sendMessage?chat_id="
        + str(bot_chatID)
        + "&parse_mode=Markdown&text="
        + bot_mesaj
    )
    try:
        response = requests.get(url)
    except Exception as e:
        print("Error:", e)

Hello @BerkayDAN welcome to the community!

How are you fetching the token/api in Replit? Since replit have a function called Secrets, the method to call those secrets is different from PyCharm, so you have to change the code a little to adapt.

If possible post the link to your repl too.

1 Like

Also, I’d recommend handling the response from the requests.get(url) . It’s better to print the response status code and text to find out what’s going wrong.
By doing something like this:

def telegram_bot_sendtext(bot_mesaj, id: str):  # Telegram bold: * * , italik: _ _
    bot_token = config.telegram_bot_token
    bot_chatID = id
    url = (
        "https://api.telegram.org/bot"
        + bot_token
        + "/sendMessage?chat_id="
        + str(bot_chatID)
        + "&parse_mode=Markdown&text="
        + bot_mesaj
    )
    try:
        response = requests.get(url)
    except Exception as e:
        print("Error:", e)
1 Like

Shouldn’t there be an extra / in url? (After https://api.telegram.org/bot)

url = 'https://api.telegram.org/bot/' + bot_token + '/sendMessage?chat_id=' + str(bot_chatID) + '&parse_mode=Markdown&text=' + bot_mesaj

2 Likes

I tried your url but unfortunately it doesn’t work.

What is Secret function ? I don’t know the differences between PyCharm. Can you explain in detail ?

There’s a good explanation here:
https://docs.replit.com/programming-ide/workspace-features/secrets

Also did you print the error as I said?
It can be good to let us know what kind of problem you have.

3 Likes

Unfortunately it doesn’t write anything about response status.

By the way, is it mandatory to use “os” library for using secrets?

And why this happening ? The same code works well on my computer.

I tried with Secrets but it does not send telegram message still :frowning:

Can you share your repl link so we can analyse whats wrong?

tv_TA - Replit

I did not see anything wrong with the code…

Can you try add error handling?
For example, modify your telegram_bot_sendtext and telegram_bot_sendphoto functions to print out more detailed information about errors:

def telegram_bot_sendtext(bot_message, id):
    bot_token = os.getenv("telegram_bot_token")
    bot_chatID = id
    url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={str(bot_chatID)}&parse_mode=Markdown&text={bot_message}"
    try:
        response = requests.get(url)
        response.raise_for_status()  # Add this line
    except Exception as e:
        print("Error occurred:", e)
        print("Response status code:", response.status_code)  # Add this line
        print("Response text:", response.text)  # Add this line


def telegram_bot_sendphoto(photo_path, id):
    bot_token = config.telegram_bot_token
    bot_chatID = id
    url = f"https://api.telegram.org/bot{bot_token}/sendPhoto"
    with open(photo_path, "rb") as photo:
        files = {"photo": photo}
        data = {"chat_id": bot_chatID}
        try:
            response = requests.post(url, files=files, data=data)
            response.raise_for_status()  # Add this line
        except Exception as e:
            print("Error occurred:", e)
            print("Response status code:", response.status_code)  # Add this line
            print("Response text:", response.text)  # Add this line

Try to send the messages again and see what appears in the console.

3 Likes