DEV Community

Discussion on: Building An E-commerce Telegram Bot Using Python and Fauna.

Collapse
 
alvaaz profile image
Álvaro Göede Rivera

Hi! I have problem when I try upload an image using Cloudinary, I don't know what happen. This is my code:

def add_media(update, context):
  send_photo = upload('http://res.cloudinary.com/demo/image/upload/couple.jpg')
Enter fullscreen mode Exit fullscreen mode

The console show:

2021-07-15 14:01:44,494 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/telegram/ext/dispatcher.py", line 555, in process_update
    handler.handle_update(update, self, check, context)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/telegram/ext/conversationhandler.py", line 626, in handle_update
    new_state = handler.handle_update(update, dispatcher, check_result, context)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/telegram/ext/handler.py", line 198, in handle_update
    return self.callback(update, context)
  File "/Users/alvarogoederivera/web/python_bot/handlers.py", line 64, in add_media
    send_photo = upload('http://res.cloudinary.com/demo/image/upload/couple.jpg')
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/cloudinary/uploader.py", line 46, in upload
    return call_cacheable_api("upload", params, file=file, **options)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/cloudinary/uploader.py", line 394, in call_cacheable_api
    result = call_api(action, params, http_headers, return_error, unsigned, file, timeout, **options)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/cloudinary/uploader.py", line 459, in call_api
    response = _http.request("POST", api_url, param_list, headers, **kw)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/request.py", line 78, in request
    return self.request_encode_body(
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/request.py", line 155, in request_encode_body
    body, content_type = encode_multipart_formdata(
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/filepost.py", line 78, in encode_multipart_formdata
    for field in iter_field_objects(fields):
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/filepost.py", line 42, in iter_field_objects
    yield RequestField.from_tuples(*field)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/fields.py", line 181, in from_tuples
    filename, data = value
ValueError: not enough values to unpack (expected 2, got 1)
Enter fullscreen mode Exit fullscreen mode

Does anyone have any idea what is going on?

Collapse
 
curiouspaul1 profile image
Curious Paul

Hi, this doesn't seem like the code i wrote in the demo, for this article, however this doesn't look like the code to upload an image to cloudinary looks like. You might want to check the docs or use the one i wrote on the product_info method in handlers.py from the tutorial.

Collapse
 
alvaaz profile image
Álvaro Göede Rivera • Edited

I get the same problem when doing the demo, that's why I simplified it.

def add_media(update: Update, context: CallbackContext):
  logger.info(f"El usuario {update.effective_user['username']}, subió foto")
  quote = context.user_data["new_quote"]
  photo = context.bot.getFile(update.message.photo[-1].file_id)
  file_ = open("product_image", "wb")
  photo.download(out=file_)
  send_photo = upload("product_image", width=200, height=150, crop='thumb')
Enter fullscreen mode Exit fullscreen mode
dp.add_handler(ConversationHandler(
  entry_points = [CommandHandler("new", handlers.start)],
  states={
    handlers.QUOTE: [
      MessageHandler(
          Filters.all, handlers.add_quote
      )
    ],
    handlers.MEDIA: [
      MessageHandler(Filters.photo, handlers.add_media)
    ]
  },
  fallbacks=[CommandHandler('cancel', handlers.cancel)],
  allow_reentry=True)
)
Enter fullscreen mode Exit fullscreen mode

The console show the same error:

ValueError: not enough values to unpack (expected 2, got 1)
Enter fullscreen mode Exit fullscreen mode

I know the error is upload because when I remove it the error does not appear.

Thread Thread
 
curiouspaul1 profile image
Curious Paul

I see, do you add caption to the image when you test it though.?

Thread Thread
 
alvaaz profile image
Álvaro Göede Rivera

Could you check my code and see if there is something I am doing wrong?

github.com/alvaaz/telegram_bot

I would really appreciate it

Thread Thread
 
curiouspaul1 profile image
Curious Paul

oh great!, thanks for making this easier I'll take a look