skip to Main Content

I have this piece of code i’m using for a telegram bot:

r = requests.post("https://api.telegram.org/botMY_BOT_ID/sendPhoto?chat_id=MY_CHAT_ID",files=stuff)
print(r.status_code)
print(r.content)

Theres any way to change the "files=stuff" to "data=stuff" depending on the type of "stuff"(bytes or string), without writing another requests line ?

2

Answers


  1. Use a dict display that evaluates an expression to generate the correct keyword.

    requests.post(..., **{kind_of_stuff(stuff): stuff})
    

    where kind_of_stuff is a function that returns 'file' or 'data' as appropriate based on the type of stuff.

    Login or Signup to reply.
  2. Maybe something like this?

    r = requests.post("...", **{"file":stuff} if //condition else {"data": stuff})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search