Development tools
Pavel Sparrow
Co-Founder, No-code Expert
November 19, 2023
A low-code platform blending no-code simplicity with full-code power 🚀
Get started free
November 19, 2023
8m
min read

Scenario for sending an email to a travel agency client who has just booked a tour

Pavel Sparrow
Co-Founder, No-code Expert
Table of contents

In this article, we will set up a script in Latenode to send an email to a travel agency client who has just booked a tour.

The scheme of such a scenario is simple: we know about the client his gender, age, from which city and to which city he is planning a trip. Based on these data, we will prepare an email text to tell him where to go and what to see when he arrives in town. Here's an example of the text that ChatGPT will prepare:

Welcome to the charming city of Limassol, Cyprus! As a winter tourist, there are plenty of things for you to see and do during your stay. When it comes to places to see, the top three must-see attractions are: The Limassol Castle, a medieval castle located in the heart of the city. The castle is a great place to learn about the city's rich history and also offers great views of the city and the sea. The Limassol Marina, a beautiful marina that offers a variety of shops, restaurants, and bars. It's a perfect place to take a walk, have a meal or a drink and admire the boats. The Limassol Old Town, a charming neighborhood filled with traditional architecture, quaint shops, and cozy cafes. This is a perfect place to stroll around and get a feel for the local culture. As for activities to do, the top three recommendations are: Visit the Limassol Archaeological Museum, which houses a vast collection of artifacts that shed light on the island's rich history and culture. Take a wine tour of the Limassol region, where you can sample the local wines and learn about the island's wine-making tradition. Visit the Limassol salt lake, which is a great place for birdwatching and also offers beautiful views of the city. Limassol is a city with a lot to offer and these are just a few of the many things you can see and do during your visit. Enjoy your stay in Limassol, and don't hesitate to ask locals for recommendations as they will be more than happy to help you discover the hidden gems of their city.

Preparing

First, let's check the list of what we need to create a chain: a ChatGPT account and its Personal key, as well as the Mailgun account and its Private API Key;

Personal key in ChatGPT can be viewed or created here in the profile settings:

The Mailgun API key is also in the settings in the profile:

Creating a chain in Latenode

The first thing we do, as always, is to create a node: "+ Add Node" => "Http" => "Webhook";

Let's copy its address at once;

Save your changes;

Let's also prepare a request for a rest client (Postman, Insomnia):

REQ for rest client:


    curl --request POST
    --url https://webhook.latenode.com/69/dev/8a3d287b-6580-45d0-8d69-c37b9a41e890
    --header 'Content-Type: application/x-www-form-urlencoded'
    --data 'personalTokenChatGPT=Your token'
    --data 'apiKeyMailgun=Your token'
    --data 'email=Your test email'
    --data 'personAge=26'
    --data 'personGender=male'
    --data 'desiredSeason=winter'
    --data 'cityDestination=Prague'

Change the Webhook link to yours;

Next, substitute in personalTokenChatGPT your key from ChatGPT;

Next, put in apiKeyMailgun your key from Mailgun;

Also change the email to your test email, so if you have a free Mailgun plan you can send only to the email you specified when registering;

The rest you can change at will;

Create a new node: "+ Add Node" => "Code" => "JavaScript";

Insert the content into it:


    const apiChatGPT = "https://api.openai.com/v1";
    const apiMailgun="https://api.mailgun.net/v3";
    const personalTokenChatGPT = `Bearer ${data["{{1.body.personalTokenChatGPT}}"]}`;
    const apiKeyMailgun = data["{{1.body.apiKeyMailgun}}"];
    const personGender = data["{{1.body.personGender}}"];
    const personAge = data["{{1.body.personAge}}"];
    const desiredSeason = data["{{1.body.desiredSeason}}"];
    const cityDestination = data["{{1.body.cityDestination}}"];
    const message = `Give me an article for a tourist about the top 3 places to see and top 3 activities to do in the city of destination. The tourist is ${personGender}, ${personAge} years old, traveling in  ${desiredSeason}, destination city ${cityDestination}.`;

    const resultRawJSON = JSON.stringify({
      "model":"text-davinci-003",
      "prompt":message,
      "temperature":0,
      "max_tokens":1400
    });

    return {
      apiChatGPT,
      apiMailgun,
      personalTokenChatGPT,
      apiKeyMailgun,
      resultRawJSON
    }

Save;

Bind the nodes together;

Start and call the chain to transfer data between the nodes;

Let's open the "JavaScript" node settings again;

Check all the constructions like "data["{{1.body.personGender}}"]" that the number of the Webhook corresponds to the one from which we receive data, if not replace it with the required one (remember, 1 is the number of the node written under the node name, then if it is a query then you have to choose the object field responsible for the type of sent/received data - body for forms or query for query parameters);

Next, create an "Http request" node that will make a request to generate sentences based on text parameters from the message variable in the JS node;

Create node: "+ Add node" => "Http" => "HTTP request";

Save;

Let's link the nodes together;

Start and call the chain to transfer data between the nodes;

Let's form the "Url" field: at the beginning we'll substitute the variable "apiChatGPT" from the "JavaScript" node, and after it we'll specify /completions;

Read more about this and other calls in ChatGPT API at the end of this article;

Let's change the method to "POST";

In "raw", let's substitute the variable from the first "JavaScript" node;

In the Headers we are going to add two:

- Content-Type with value: application/json
- Authorization, in the value select a variable from the "JavaScript" node with the name: personalTokenChatGPT

Let's save the changes;

Add a "Webhook response" node to return the ChatGPT-generated text in the response: "+ Add Node" => "Http" => "Webhook response";

Save;

Bind the nodes together;

Start and call the chain to transfer data between the nodes;

Let's add the response received from the previous node "Http request", don't forget to specify the index between the square brackets;

Let's save the changes;

Last let's add one more node "Http request" to send the generated ready marketing offers through Mailgun: "+ Add node" => "Http" => "HTTP request";

Save;

Bind the nodes together;

Run and call the chain to transfer data between the nodes;

Next, let's go to the Mailgun website to the section: "Sending" => "Overview";

Copy the ready-made CURL code:

Paste it into the "Http request" via "Create from example (CURL)":

Then copy the key and domain from the Mailgun site somewhere, like a notepad;

In the Url we first put the apiMailgun variable from the first "JavaScript" node, then /your domain in Mailgun/, at the end /message;

Change the method to "POST";

In the body we will select the tab "form-data";

In the body we'll do everything like in the screenshots below:

- from: Mailgun Sandbox <postmaster@your domain in Mailgun>;
- To: your test mailbox to test, select the "JavaScript" node variable clientEmail, put it inside <>;
- subject: Time to trip?;
- text: variable from the "Webhook response" node received text after generation;

Below in the "Authorization" block by the "Password" field, insert the apiKeyMailgun variable from the first "JavaScript" node;

Save the changes;

Let's start a full chain and call it;

After that you should get a letter with the generated text to the email address you specified, check your spam folder if you haven't found it in your inbox;

Thanks for your attention, see you in new articles, there will be another one on ChatGPT! 😉

Links:


Basic:

Latenode

ChatGPT

ChatGPT API Key

Mailgun

Mailgun API-Key

Information:

ChatGPT API Documentation

Mailgun API Documentation

Application OneApplication Two

Try now

Related Blogs

Use case

Backed by