0

I'm trying to set up a few API calls to Twilio's Conversations Service so I can connect CRM data in Google Sheets to Twilio's Frontline App. I'm struggling to successfully modify a participant.

I try to call Twilio's Edit Conversation Participant API using the code below.

function editParticipantAttributes() {
  
  var twUrl = 'https://conversations.twilio.com/v1/Conversations/' + chSid + '/Participants/' + mbSid;
  
  var options = {
    method: 'post',
    headers: {
      'Authorization': twAuthHeader,
    },
    followRedirects: true,
    muteHttpExceptions: true,
    payload: {attributes: JSON.stringify({
      customer_id: '1',
      display_name: 'First Last'
      })
    }
  }

  var response = UrlFetchApp.fetch(twUrl, options);
  Logger.log(response)
}

The Twilio Conversations Resource uses the following cURL as an example:

curl -X POST "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
--data-urlencode "DateUpdated=2019-05-15T13:37:35Z" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Here's the info on the attributes parameter:

An optional string metadata field you can use to store any data you wish. The string value must contain structurally valid JSON if specified. Note that if the attributes are not set "{}" will be returned.

I get a response back with information about the appropriate participant, but attributes remains "{}". Can anyone please help me see my mistake?

Response:

{"last_read_message_index": null, "date_updated": "2023-05-25T00:45:09Z", "last_read_timestamp": null, "conversation_sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "url": "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "date_created": "2023-05-24T23:05:37Z", "role_sid": "RLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "sid": "MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "attributes": "{}", "identity": null, "messaging_binding": {"proxy_address": "+1XXXXXXXXXX", "type": "sms", "address": "+1XXXXXXXXXX"}}

Thanks in advance!

  • I have to apologize for my poor English skill. Unfortunately, I cannot understand your question. Can I ask you about the relationship between your Google Apps Script and your curl command and your expected result? And, can you provide the official document of the method of the API you want to use? – Tanaike May 25 '23 at 00:56
  • Hi Tanaike. Apologies. First of all, let me post the correct curl command. This is from Twilio's API: ATTRIBUTES=$(cat << EOF { "role": "driver" } EOF ) curl -X POST "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \ --data-urlencode "Attributes=$ATTRIBUTES" \ -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN Here's the official document (looking at update attributes for a conversation Participant) : https://www.twilio.com/docs/conversations/api/conversation-participant-resource – Patterson Park Music May 25 '23 at 01:03
  • Thank you for replying. Where can I see `ATTRIBUTES=$(cat << EOF { "role": "driver" } EOF ) curl -X POST "conversations.twilio.com/v1/Conversations/…" \ --data-urlencode "Attributes=$ATTRIBUTES" \ -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN` in your provided official document? And, what do you want to do about this curl command? Unfortunately, I cannot still understand the relationship between your Google Apps Script and your curl command, and your expected result. I apologize for this. – Tanaike May 25 '23 at 01:14
  • You can see it here: https://www.twilio.com/docs/conversations/api/conversation-participant-resource?code-sample=code-update-attributes-for-a-conversation-participant&code-language=Node.js&code-sdk-version=4.x I'm trying to make my apps script accomplish the same thing as the curl command. I am trying to update the participant's attribute field to include a customer_id and a display_name – Patterson Park Music May 25 '23 at 01:24
  • Thank you for replying. From your reply, I proposed an answer. Please confirm it. If that was not useful, I apologize. – Tanaike May 25 '23 at 01:34

1 Answers1

0

I believe your goal is as follows.

  • You want to convert the following curl command to Google Apps Script.

    ATTRIBUTES=$(cat << EOF
    {
        "role": "driver"
    }
    EOF
    )
    
    curl -X POST "https://conversations.twilio.com/v1/Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/MBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
    --data-urlencode "Attributes=$ATTRIBUTES" \
    -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
    

In this case, how about the following modification?

Modified script:

function editParticipantAttributes() {
  var TWILIO_AUTH_TOKEN = "TWILIO_AUTH_TOKEN"; // Please set your value.
  var TWILIO_ACCOUNT_SID = "TWILIO_ACCOUNT_SID"; // Please set your value.
  var obj = { "role": "driver" };

  var twUrl = 'https://conversations.twilio.com/v1/Conversations/' + chSid + '/Participants/' + mbSid;
  var basicAuth = Utilities.base64Encode(`${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}`);
  var options = {
    method: 'post',
    headers: { 'Authorization': `Basic ${basicAuth}` },
    payload: { "Attributes": JSON.stringify(obj) },
    muteHttpExceptions: true,
  }
  var response = UrlFetchApp.fetch(twUrl, options);
  Logger.log(response.getContentText())
}

Note:

  • I think that this request is the same as the above curl command. But, unfortunately, I cannot test this modified script. So, when you test this script and an error occurs, please confirm your values TWILIO_AUTH_TOKEN, TWILIO_ACCOUNT_SID, obj, and twUrl, again.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Patterson Park Music Did my answer show you the result that you want? Would you please tell me about it? That is also useful for me to study. If this works, other people who have the same issue as you can also base your question as a question that can be solved. If you have issues with my answer yet, I apologize. At that time, can I ask you about your current situation? I would like to study to solve your issues. – Tanaike May 26 '23 at 23:59