Generating Engagement for Indie Games with OpenAPI + Twitter API + Azure Logic Apps (EN/US)
Tags: openai, twitter, azure, gamedev
Published: 2023-05-26 16:51:05

## Briefing

In summary, game development is not my main activity. It's a hobby that I mainly pursue during my free time at [DZ9 Games](https://www.dz9games.com/), during sleepless nights, or on weekends. However, I realized that this journey was becoming impossible to pursue alone, so I decided to invite an illustrator and a scriptwriter to join the #theagent team, both of whom eagerly accepted the challenge.

If you want to know more details about the game and its progress, you can find them [here](https://linktr.ee/dz9games), [here](https://theagent.dz9games.com/), or [here](https://www.instagram.com/theagent.game/).


## The Problem

As the Demo version (which is currently in progress) approaches, we are starting to work on social media and explore potential marketing channels. However, we face a challenge as nobody on the team is dedicated full-time to this role. The time required for this "**marketing**," "**advertising**," and "**engagement**" work directly conflicts with tweaking game mechanics, artwork, and overall improvements.

Once again, it is impossible to juggle and accomplish all these tasks with a minimum level of quality.

## The Solution Was Close

During the development phase, we faced the need to solve our marketing challenges. We considered various options, such as hiring an agency, a social media specialist, or even an intern, but these alternatives mainly involved additional costs 😭. With every new idea, it seemed like the impossible always stood before us.

However, a simple and unassuming suggestion emerged and proved to be perfect: "What if we automate this somehow?" Most social media platforms provide APIs and integration methods for post publishing, but creative work and the team's time were still required.

## AI as a Facilitator!
By witnessing the rapid evolution of AI on a daily basis, it became evident how it can streamline the resolution of our problems. In this tutorial, I will demonstrate how we implemented the automatic and efficient generation of tweets on our profile.

It's important to emphasize that despite automating tweet generation, we still actively monitor and respond to all comments. So, if you come across posts on our [Twitter profile](https://twitter.com/dz9_games), rest assured that we are there! 😄😄😄

## Step 1 - Twitter Developer Platform

The first step was to activate our account on the developer portal and create a project in the free tier, which allows us to generate approximately 1500 tweets per month. Then, we selected the read and write option under App permissions, and proceeded to create an application to generate the authentication keys: Consumer Keys/API Key and Secret, and Authentication Tokens/Access Token and Secret.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f216qax99vtuqkl9ibrf.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkqgk0b5bbpwgeyxddkg.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6h08azl0v0p03e3tfjm2.png)

## Step 2 - OpenAI API

The second step involves generating the access key for your OpenAI account. When you create an account, you will receive a $5 credit for testing API requests. Later on, you will need to acquire a plan to access the public APIs.

Once you have the access key, it's time to prepare the request for  [v1/chat/completions](https://platform.openai.com/docs/api-reference/chat/create).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcm9yad5mm8mrh4eqvkv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2jlx3ukuj8xo68n1ru4.png)

## Step 3 - Preparing the Model

Before we proceed with the API requests, we can prepare our model directly in the browser at [chat.openai.com](https://chat.openai.com/).
Use this [template](https://gist.github.com/leduqueiroz/92d1c1015762aa53af0e1d8f09f62619) and insert the information about your game. Provide details and make adjustments multiple times until you achieve a satisfactory result.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnshzd4im1n56czkh5mh.png)

## Step 4 - Postman + OpenAPI

Import the curl template into Postman, and replace the values with:

**- API KEY**: Replace this value with the API key you generated in Step 
**- ENCODED TEMPLATE**: Format your refined prompt text from Step 3 using an online Escape/Unescape tool. Replace this value with the formatted text.
**- YOUR MODE**L: List all the generated models from the browser in Step 3 that met your needs. Include them here without any limitations. This will help refine and make the response more aligned with your request.
**- YOUR REQUEST**: Simply provide your request here, specifying the type of tweet you need. For example: "I need a tweet," "I need a tweet in French," or "I need a tweet in English."

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4yfcqhof4zsnedeaj53.png)

## Step 5 - Postman + Twitter

Import the request **api.twitter.com/2/tweets** from the [collection](https://www.postman.com/twitter/workspace/twitter-s-public-workspace/request/9956214-5bd6ebb1-9d79-4456-a9a6-22ead4a41625).

Replace the values generated in Step 1 in the Authorization tab:

**- Consumer Key:** Key generated in the Consumer Keys/API Key and Secret group
**- Consumer Secret: **Key generated in the Consumer Keys/API Key and Secret group
**- Access Token:** Key generated in the Authentication Tokens/Access Token and Secret group
**- Access Token Secret:** Key generated in the Authentication Tokens/Access Token and Secret group

Next, fill in the request body with the world's most famous test string: _"Hello World"_ and click on _"Send"_.

When the magic happens, your profile will be fully integrated with the APIs.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk6tedjkxaqas3jy1x13.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlv9yn0o0pqkmya5aeq1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g7clbdxnhyb9od1yjrc2.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g7w2cnn2mdworns6tpd.png)

## Step 6 - Azure AppLogic

Access your account through the Azure Portal and create a new Logic App.

After deploying the application, click on the "Design" option. This is where we will start configuring the recurrence of tweets.

Select _'Blank App Logic / Scheduler / Recurrence'_. On the next screen, define the desired time interval. For this example, since we are using free/trial tiers, it is recommended to set the execution between 6 and 12 hours.

Now, create a new step of type _'HTTP'_ and copy the values used in Postman, as shown in Step 4. At this point, it is important to remember to add the access authorization, as demonstrated in the images below. (If you encounter difficulties with this task, you can use the file located [here ](https://gist.github.com/leduqueiroz/7ebfd1f79c662efb600fc55bab15a052) to configure it).

The next task is of type _'Data Operations/Parse JSON'_. Here, we need to define the structure of the API response. Select the option _'Use sample payload to generate schema'_ and past your response body to automatically map the structure.

With the response structure ready, let's manipulate the data. Create a task of type _'Control/For each'_, selecting the '**choices**' node (where the fields we need are located). Then, create a task of type _'Control/Condition'_ and validate whether the '**message**' field is filled or not.

If the field is not filled, no action is required. However, you can customize this task: redo the steps, send an email, or send a predefined tweet. The choice of what to do in this flow depends on your desire/need. In my case, I simply end the execution.

If the '**message**' field is correctly filled, the last step is to send the request to the Twitter API. Create a new task of type _'HTTP'_ and fill in the same values used in Step 5 (configuration of the task [here](https://gist.github.com/leduqueiroz/ff0c96386dc2dcfcac696e18d26b3b54), if necessary). Don't forget to fill in the authentication values.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wjjdvvn8756jo7soprgq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0f79km44z5gkxhg12bu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbi9gn0yl9uoq844p2s7.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0vw7bui41ppdav6h503.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpa4xhwupbjetji1dmwf.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szcicu9ejwszmfnr58v9.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scwkx7rhiusn0ortaixv.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylq95kpv0b9ko01o8bwb.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xgksf32v6mm4hybfy1o.png)

## Step 7 - Testing

To test your application, simply select the _'Run Trigger'_ option and monitor the execution log to check the status. In case of successful execution, just access your Twitter profile to see the update.

Remember that, according to the time configured in step 6, you will have a new post!

Important:

You may need to continue refining your model by including new information and adjusting the values of temperature, top_p, frequency_penalty, presence_penalty, and max_tokens. To understand these parameters, you can refer to the OpenAPI documentation, test them in Postman, and then update your HTTP task in step 6.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opzuz5sgjpq7x3b9vqqt.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ov2x5teolhmkko56x5y.png)

## Step 8 - Final Considerations

In this article, I aimed to provide detailed steps so that non-developers or individuals without familiarity with cloud environments or programming languages can easily configure the tasks!

If you want to learn more about the game "The Agent - A Role Playing Game" and its development, you can find more details [here](https://linktr.ee/dz9games), [here](https://theagent.dz9games.com/), or [here](https://www.instagram.com/theagent.game/).

To stay updated with our tweets, you can follow our page [here](https://twitter.com/dz9_games).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28gwt83ciu6rvt5z648d.png)

Furthermore, if you have any questions during the process, feel free to reach out. I'm available to assist and clarify any doubts that may arise.

I hope this detailed guide proves useful in keeping the independent gaming community engaged and up-to-date with the latest developments. :video_game:

Together, we can drive the growth and success of this passionate community! :heart_eyes: :heart:

Best regards!