Is there a way to generate URLs for local files?

1.5k Views Asked by At

I'm trying to create a Viber chat-bot that is able to send some pictures. I am required to send URL of an image as a parameter, but the images I want to send are on my desktop and I do not know how to get their URLs.

I tried using requests:

image = {  "media": open('test_image.jpg', 'rb')}

payload = { 
  "auth_token": "my token",
  "receiver": "my chat id", 
  "type": "picture", 
  "text": "some text", 
}

requests.post("https://chatapi.viber.com/pa/send_message", json=image, file=image)

I also tried using imgur API but it is a pain getting "pin" authentication all the time using selenium.

Here is my code:

from viberbot import Api
from viberbot.api.bot_configuration import BotConfiguration

from viberbot.api.messages.picture_message import PictureMessage
from viberbot.api.viber_requests import ViberMessageRequest

from flask import Flask, request, Response

app = Flask(__name__)

bot_configuration = BotConfiguration(
    name='name',
    avatar='http://viber.com/avatar.jpg',
    auth_token='token'
)
viber = Api(bot_configuration)


@app.route('/', methods=['POST'])
def index():
  viber_request = viber.parse_request(request.get_data())

  if isinstance(viber_request, ViberMessageRequest):
    message = viber_request.message

    if message.text == "/pic":

      viber.send_messages(viber_request.sender.id, [
        PictureMessage(media="image url", text="Some text")
      ])

  return Response(status=200)


if __name__ == '__main__':
  app.run(debug=True)

To sum up: In picture message object PictureMessage(media="image url", text="Some text") "media" has to be URL of image (only JPEG supported). How would you turn images on your desktop into URLs?

1

There are 1 best solutions below

0
On

Generate a file URI such as file:///C:/Users/bob/Desktop/avatar.jpg .

How you generate that whole path depends on where you expect these files to be. But this is the syntax:

https://en.wikipedia.org/wiki/File_URI_scheme

You should read it and note the 3 slashes before the path, and understand why (because it's shorthand for file://localhost/C:/Users/bob/Desktop/avatar.jpg).