27

i'm just implementing a simple bot who should send some photos and videos to my chat_id. Well, i'm using python, this is the script

import sys
import time
import random
import datetime
import telepot

def handle(msg):
    chat_id = msg['chat']['id']
    command = msg['text']

    print 'Got command: %s' % command

    if command == 'command1':
        bot.sendMessage(chat_id, *******)
    elif command == 'command2':
        bot.sendMessage(chat_id, ******)
    elif command == 'photo':
        bot.sendPhoto(...)

bot = telepot.Bot('*** INSERT TOKEN ***')
bot.message_loop(handle)
print 'I am listening ...'

while 1:
    time.sleep(10)

In the line bot.sendphoto I would insert the path and the chat_id of my image but nothing happens.

Where am I wrong?

thanks

Kraay89
  • 919
  • 1
  • 7
  • 19
rollotommasi
  • 461
  • 1
  • 6
  • 11

7 Answers7

34

If you have local image path:

bot.send_photo(chat_id, photo=open('path', 'rb'))

If you have url of image from internet:

bot.send_photo(chat_id, 'your URl')
Majid A
  • 802
  • 9
  • 19
23

Just using the Requests lib you can do it:

def send_photo(chat_id, file_opened):
    method = "sendPhoto"
    params = {'chat_id': chat_id}
    files = {'photo': file_opened}
    resp = requests.post(api_url + method, params, files=files)
    return resp

send_photo(chat_id, open(file_path, 'rb'))
jarh1992
  • 603
  • 7
  • 8
9

I have used the following command while using python-telegram-bot to send the image along with a caption:

 context.bot.sendPhoto(chat_id=chat_id, photo=
"url_of_image", caption="This is the test photo caption")
Manchana
  • 139
  • 3
  • 8
7

I've tried also sending from python using requests. Maybe it's late answer, but leaving this here for others like me.. maybe it'll come to use.. I succeded with subprocess like so:

def send_image(botToken, imageFile, chat_id):
        command = 'curl -s -X POST https://api.telegram.org/bot' + botToken + '/sendPhoto -F chat_id=' + chat_id + " -F photo=@" + imageFile
        subprocess.call(command.split(' '))
        return
Cipri
  • 71
  • 1
  • 3
7

This is complete code to send a photo in telegram:

import telepot
bot = telepot.Bot('______ YOUR TOKEN ________')

# here replace chat_id and test.jpg with real things
bot.sendPhoto(chat_id, photo=open('test.jpg', 'rb'))
Biplob Das
  • 2,818
  • 21
  • 13
2

You need to pass 2 params

bot.sendPhoto(chat_id, 'URL')
RuralGalaxy
  • 121
  • 6
2

sendPhoto requires at least two parameters; first one is target chat_id, and for second one photo you have three options:

  1. Pass file_id if the photo is already uploaded to telegram servers (recommended because you don't need to reupload it).
  2. If the photo is uploaded somewhere else, pass the full http url and telegram will download it (max photo size is 5MB atm).
  3. Post the file using multipart/form-data like you want to upload it via a browser (10MB max photo size this way).
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
Arash
  • 639
  • 2
  • 9
  • 15