skip to Main Content

I got a string in this form

payload = ["Text 1", "Text 2"]

I want to use Text 2 as an object. How can I return it?

UPDATE
I’m making a function which returns a generic template Facebook API.
The first payload works well, but I want to return a string and a object in the second payload ( result )

button = [
            {
                "type": "postback",
                "title": "Buy item",
                "payload": "Buy this item: " + (product['id'])
            },
            {
                "type": "postback",
                "title": "Add to wishlist",
                "payload": result
            }
        ]

My second payload should look like this:

payload = {
            'Order_item',
            product['title']
        }

Because I got this error [buttons][1][payload] must be a UTF-8 encoded string so I did convert it and it returns a STRING in this form ["Order_item", "Ledverlichting Fiets Blauw"]

Because I want that when a Facebook user clicks on a postback ( Add to wishlist ), the product['title'] value will be save in Django database. And product['title'] is the Text 2 in the question above.

4

Answers


  1. You need to split the string then trim and keep splitting/trimming to get all parts that you want to have in list

    s = 'payload = ["Text 1", "Text 2"]'
    items = s.strip()[1:-1]
    #if 'payload = ' is also part of string you need to split it also by '=' so it would be:
    #items = s.split("=").strip()[1:-1] 
    l = [item.strip()[1:-1] for item in items.split(",")]
    

    then you have list of string that you can iterate, get n-th item and so on

    the_item = l[1]
    return the_item
    
    Login or Signup to reply.
  2. you can try:

    >>> payload = ["t1", "t2"]
    >>> t2 = [1,2]
    >>> eval(payload[1])
    [1, 2]
    
    Login or Signup to reply.
  3. assuming that you want a string object, you can get the specific index in the array(first spot = 0, second spot = 1, etc).

    you can save the string object contained in the array like this:

    payload = ["Text 1", "Text 2"]
    s = payload[1]
    

    and than you can return the s object.

    Login or Signup to reply.
  4. I had a string that was in strictly list format and simply passing it through eval() worked.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search