3

I need to send a link to a CloudPage through SMS with an query param (www.example.com?id=XXXX), which the CloudPage will use to get value from a Data Extension and pre-fill some data to Contacts and ask for others.

This param value is different for each contact and the actual URL is too big, so I want to use Bitly to short this link. I can use AMPScript to create a dynamic link with Bitly, but this example is using V3 API, since it's going to be deactivated, I'm making it in V4.

V3 will be deactivated on March

The problem now it's I need to make an request through Bit.ly API with an JSON in request body, inside SMS message.

%%[
  var @testID, @cloudpage, @link, @leadId, @bitlyPage, @contentType, @content,
      @bitlyToken, @additionalHeaderName, @additionalHeaderValue, @output, @respheader
  set @testID = 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
  set @bitlyToken = 'myToken'
  set @bitlyPage = 'https://api-ssl.bitly.com/v4/bitlinks'
  set @contentType = 'application/json'
  set @additionalHeaderName = 'Authorization'
  set @additionalHeaderValue = concat('Bearer ', @bitlyToken)


  set @cloudpage = CloudPagesURL(1591,'id',@testID)
  set @link = URLEncode(@cloudpage)

  set @content = Stringfy({"long_url": @link})
]%%

  %%=HTTPPost2(@bitlyPage, @contentType, @content, true, @output,
        @respheader, @additionalHeaderName, @additionalHeaderValue)=%%

  Output: %%=v(@output)=%%
  Header: %%=v(@respheader)=%%

When I try to run this in Preview and test, I get the following error:

There is an error in your email. Please contact your customer service representative. Error 1: Script SET Statement Invalid
An error occurred when attempting to resolve a script expression. See inner exception for detail. Script Expression: Stringfy MemberID: nnnnnnn JobID: 0 An unrecognized expression appears in a script block.

If I try without Stringfy, I get this error:

Error 1: Script SET Statement Invalid
An error occurred when attempting to resolve a script expression. See inner exception for detail.

How can I send this JSON data in body? or there is another way to make this Bitly link?

3

Stringify isn't an AMPscript function. You should be able to use concat instead:

%%[

  var @testID, @cloudpage, @link, @leadId, @bitlyPage, @contentType, @content,
  @bitlyToken, @additionalHeaderName, @additionalHeaderValue, @output, @respheader

  set @testID = 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
  set @bitlyToken = 'myToken'
  set @bitlyPage = 'https://api-ssl.bitly.com/v4/bitlinks'
  set @contentType = 'application/json'
  set @additionalHeaderName = 'Authorization'
  set @additionalHeaderValue = concat('Bearer ', @bitlyToken)


  set @cloudpage = CloudPagesURL(1591,'id',@testID)
  set @link = URLEncode(@cloudpage)

  set @content = concat('{"long_url": ', @link, '}')

]%%

%%=HTTPPost2(@bitlyPage, @contentType, @content, true, @output, @respheader, @additionalHeaderName, @additionalHeaderValue)=%%

Output: %%=v(@output)=%%
Header: %%=v(@respheader)=%%
| improve this answer | |
0

Thanks to Adam answer, I was able to send the request to Bitly v4 API, but I had other problem: The response was a JSON and I was unable to get the "Link" attribute and use it in the message through AMPScript.

I saw that Guide Template Language could help me to read the JSON field.

So was able to do all the work, generate the link, get it and wirte the message using the following code (also here!).

%%[
    var @message, @link, @param, @bitlyPage, @contentType, @content,
        @bitlyToken, @additionalHeaderName, @additionalHeaderValue, @request, @response
    set @bitlyToken = 'XXXXXXXXXXXXXXXXXXXX'
    set @bitlyPage = 'https://api-ssl.bitly.com/v4/bitlinks'
    set @contentType = 'application/json'
    set @additionalHeaderName = 'Authorization'
    set @additionalHeaderValue = concat('Bearer ', @bitlyToken)

    set @param = AttributeValue('attribute')

    set @link = concat('https://yourlink.com.br/something?id=', @param)

    set @content = concat('{"long_url": "',@link,'"}')

    set @request = HTTPPost(@bitlyPage, @contentType, @content, @response, 
      @additionalHeaderName, @additionalHeaderValue)

    set @message = 'Hello, here is the link '
  ]%%%%=v(@message)=%%{{.datasource responsevar type=variable}}{{.data}}{ "target" : "@response" }{{/data}}{{link}}{{/datasource}}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.