Yash Dwivedi
Yash Dwivedi

Reputation: 47

Not able to send Images via Telegram Bot

When I give the command, it just doesn't respond. All my other commands are working fine. I am using pytelegrambotapi.

My code-

import telebot
from PIL import Image
import requests
from io import BytesIO
    
#This is my image link
IMAGE_LINK = "https://pixabay.com/images/id-1127657/"
    
@bot.message_handler(commands=['image'])
def image(message):
    response = requests.get(IMAGE_LINK)
    img = Image.open(BytesIO(response.content))
    #send the photo
    bot.send_photo(message.chat.id, img)

Upvotes: 0

Views: 902

Answers (1)

Ali Padida
Ali Padida

Reputation: 1939

Your image url is not correct, it goes to a page with other elements along with the image itself. The correct url for your image is: https://cdn.pixabay.com/photo/2016/01/08/11/49/text-1127657_960_720.jpg

Also you can pass this link to send_photo directly, that way Telegram itself will download and send the photo from the url:

IMAGE_LINK = "https://cdn.pixabay.com/photo/2016/01/08/11/49/text-1127657_960_720.jpg"


@bot.message_handler(commands=['image'])
def image(message):
    bot.send_photo(message.chat.id, IMAGE_LINK)

Upvotes: 2

Related Questions