Skip to content

[FEATURE] Handle bytes input for files #2230

@0xAhmad

Description

@0xAhmad

Steps to reproduce

Deploy the following code on Heroku:

import requests
import telegram

bot = telegram.Bot("<TOKEN>")
update = telegram.Update.de_json(request.get_json(force=True), bot)
chat_id = update.message.chat.id
response = requests.get('https://source.unsplash.com/random')

bot.send_photo(chat_id, response.content)

Expected behaviour

Sends an image to the bot.

Actual behaviour

The app crashes.

Configuration

Heroku

Version of Python, python-telegram-bot & dependencies:

$ python -m telegram
python-telegram-bot 13.1
certifi 2020.11.08
Python 3.6.12 (default, Aug 18 2020, 11:10:29) [GCC 7.5.0] 

Logs

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/app/app.py", line 9, in mybot_respond
    return mybot.respond()
  File "/app/bots/mybot.py", line 30, in respond
    bot.send_photo(chat_id, response.content) 
  File "<decorator-gen-5>", line 2, in send_photo
  File "/app/.heroku/python/lib/python3.6/site-packages/telegram/bot.py", line 135, in decorator
    result = func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/telegram/bot.py", line 639, in send_photo
    api_kwargs=api_kwargs,
  File "/app/.heroku/python/lib/python3.6/site-packages/telegram/bot.py", line 281, in _message
    result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/telegram/bot.py", line 245, in _post
    return self.request.post(f'{self.base_url}/{endpoint}', data=data, timeout=timeout)
  File "/app/.heroku/python/lib/python3.6/site-packages/telegram/utils/request.py", line 352, in post
    body=json.dumps(data).encode('utf-8'),
  File "/app/.heroku/python/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/app/.heroku/python/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/app/.heroku/python/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/app/.heroku/python/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy