-3

I have my bot working by now, but the thing is it can only send text. I have seen in the Bot API there are functions to send photos, videos... but I can't get it to work. Someone has achieved it? I'm using python source code from yukuku/telebot

 elif text == '/image':
            img = Image.new('RGB', (512, 512))
            base = random.randint(0, 16777216)
            pixels = [base+i*j for i in range(512) for j in range(512)]  # generate sample image
            img.putdata(pixels)
            output = StringIO.StringIO()
            img.save(output, 'JPEG')
            reply(img=output.getvalue())

When I change the code, nothing happened.

img = Image.open('image.png')
img.show()

Please help me. I need the correct code. Sorry for my bad English.

Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
Prog MuStafa
  • 79
  • 1
  • 1
  • 8
  • "change the code"... meaning what? Which **specific** lines of the above did you replace with the below? All of them? Just a few? Also, consider providing a MCVE (per http://stackoverflow.com/help/mcve) -- a **verifiable**, **complete** minimal reproducer. Also, what does "nothing happened" mean? No change from stock behavior? No response sent at all? – Charles Duffy Aug 06 '15 at 16:34
  • I am bad in English And I want to send images to users when they give me the name For example Sends word dog Bot sends a picture of a dog can you help me – Prog MuStafa Aug 06 '15 at 16:46
  • Fix the problems in your post, as identified above. Pointing those problems out so you can correct them is *how* I'm attempting to help you. – Charles Duffy Aug 06 '15 at 16:54
  • I want to make my bot like this bot https://web.telegram.org/#/im?p=@ImageBot – Prog MuStafa Aug 06 '15 at 17:04
  • how can change this code elif text == '/image': img = Image.new('RGB', (512, 512)) base = random.randint(0, 16777216) pixels = [base+i*j for i in range(512) for j in range(512)] # generate sample image img.putdata(pixels) output = StringIO.StringIO() img.save(output, 'JPEG') reply(img=output.getvalue()) to send image like this http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image2.jpg – Prog MuStafa Aug 06 '15 at 17:12
  • Edit your question to address the questions I asked in my first comment. – Charles Duffy Aug 06 '15 at 17:26
  • I have tried to change the text I hope understand me – Prog MuStafa Aug 06 '15 at 21:05
  • The new text still does not answer the questions asked. The concerns are around content, not grammar. – Charles Duffy Aug 06 '15 at 22:15
  • I'm sorry but I can not explain more What is required is that the bot sends images when requested by him and the image is incorrect – Prog MuStafa Aug 07 '15 at 10:26
  • StackOverflow is not a site where you tell us your goal and we write your code for you, so improving explanation of your goal without resolving other deficiencies is of no help. Go back and reread my first comment, and address what it asks -- **not** simply improving the grammar around your statement of your program's intent, but adding additional information to address its questions. Showing only those two lines does not tell us how you integrated them into the rest of the logic, and "does not work with me" does not tell us how it fails. – Charles Duffy Aug 07 '15 at 13:13

4 Answers4

8

I have included two functions, one is good for sending local images, the other one is good for sending remote images.

def sendImage():
    url = "https://api.telegram.org/bot<Token>/sendPhoto";
    files = {'photo': open('/path/to/img.jpg', 'rb')}
    data = {'chat_id' : "YOUR_CHAT_ID"}
    r= requests.post(url, files=files, data=data)
    print(r.status_code, r.reason, r.content)

def sendImageRemoteFile(img_url):
    url = "https://api.telegram.org/bot<Token>/sendPhoto";
    remote_image = requests.get(img_url)
    photo = io.BytesIO(remote_image.content)
    photo.name = 'img.png'
    files = {'photo': photo}
    data = {'chat_id' : "YOUR_CHAT_ID"}
    r= requests.post(url, files=files, data=data)
    print(r.status_code, r.reason, r.content)
apadana
  • 13,456
  • 15
  • 82
  • 98
3

The solution is

elif 'Hi' in text:
reply(img=urllib2.urlopen('img url').read())

or

if text == 'help':
            reply(img=urllib2.urlopen('img url').read())
Prog MuStafa
  • 79
  • 1
  • 1
  • 8
  • Please add some explanation. Imparting the underlying logic is more important than just giving the code, because it helps the OP and other readers fix this and similar issues themselves. – CodeMouse92 Oct 02 '15 at 02:20
  • get the code from here https://github.com/yukuku/telebot and reply the msg like this reply(img=urllib2.urlopen('img url').read()) – Prog MuStafa Oct 02 '15 at 09:30
2

Before sending the photo, you have to do output.seek(0) to put the cursor back to the beginning of the file, else it will be read as zero

0

I understand the question. Here's the answer:

        def sendImageFromUrl(url):
            #this tweak added if request image failed
            headers = {'user-agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'}
            response = requests.get(url, headers=headers)
            #response = requests.get(url)
            output = StringIO(response.content)
            img = Image.open(output)
            img.save(output, 'JPEG')
            resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                ('chat_id', str(chat_id)),
                ('caption', 'Your Caption'),
            ], [
                ('photo', 'image.jpg', output.getvalue()),
            ])

Make sure your server does have python module: requests.

You can download here: https://pypi.python.org/pypi/requests#downloads

And put in your application like this

/myapp/app.yaml
/myapp/main.py
/myapp/requests/packages/
/myapp/requests/__init__.py
/myapp/requests/adapters.py
etc...

Credit: https://stackoverflow.com/a/17128168/1097372

Put in main.py after line 10

import requests
from StringIO import StringIO
Community
  • 1
  • 1
Iyas
  • 520
  • 1
  • 11
  • 40