Collaboration Of Twitter&OpenAI API
Tags: programming, computerscience, python, javascript
Published: 2022-03-02 01:25:43

**Short Story**



This story happened to me in early November 2021. I was watching YouTube. I liked to watch weird Tom Scott videos.
Tom is a programmer from London, UK. Ex: Watch this -> 💯 [Recommended](https://www.youtube.com/watch?v=BxV14h0kFs0 )

This video shows the number of views in the title of the video. That even sounds fun! At that time, I was a kind of supa junior in programming and hadn't any idea how it was working? I found this great video explanation by [nang](https://www.youtube.com/watch?v=X4xtZv5nFIk). I thought it would be great to do the same thing but on Instagram Reels. As always, how? I started searching for Instagram API:
- [Closed](https://www.instagram.com/developer/). 
- [New one](https://developers.facebook.com/docs/instagram-api/)

We also can use this great tool: [RapidAPI](https://rapidapi.com/hub).

Because of some weird explanations, Instagram closed access to this type of API on their platform. It wasn't a big problem because I could research and create a new post with my automation code on AppleScript/python/js, but it sounds like a different blog idea and a waste of time. I decided to switch platforms to Twitter. 

My idea changed a little bit too. The initial plan was simple to change a title, but after a few days of thinking, I decided to make something like that :

> -  Create Twitter Post. 
> -  Get this tweet.
> -  Analyze this post. 
> -  Post request to OpenAI GPT3 with the information from this post.
> -  Get response from OpenAI.
> -  Post this response like a reply to the initial tweet. 
> -  Get the reply and so on.

#### - Documentation. 

First of all, thank you, the Twitter team, for such good [documentation](https://github.com/twitterdev)! 

Our step 0 is to get all the necessary keys.

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


Register [here](https://developer.twitter.com/en/docs/twitter-api):(and get all your secrets)


[Secrets](https://developer.twitter.com/en/portal/dashboard):
 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ideisw8dsh1e6yvk127w.png)

 


#### - Create a tweet.

The process to create a tweet is easy! But! Not for me. I spent a lot of time crying because my Node.js code had a bug or something in it. I even connected to one of the Twitter developers related to this code and asked what could be wrong. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55cc7hl2uyoumetjn9k6.png)

After a small conversation, I've decided to change to python for the Get tweet part because of many reasons: [here is a repo and code](https://github.com/aserputov/postTweet)


```
//Just a fragment.
from requests_oauthlib import OAuth1Session
import os
import JSON

# In your terminal please set your environment variables by running the following lines of code.
# export 'CONSUMER_KEY'='<your_consumer_key>'
# export 'CONSUMER_SECRET'='<your_consumer_secret>'

consumer_key = "KEY"
consumer_secret = "SECRET"

payload = {"text": "Hello world!"}

//CODE IS HUGE visit GitHub.
```
But luckily it worked: 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfgpblp1cul49ya5ziys.png)
 
#### - Get this tweet.

It's funny, but that part was much more straightforward. [Code, here.](https://github.com/aserputov/getTweetApi)

```
var axios = require("axios");

var config = {
  method: "get",
  url: "https://api.twitter.com/2/tweets/1456335161377927175?tweet.fields=created_at,attachments&expansions=author_id",
  headers: {
    Authorization: "Bearer code HERE ",
    Cookie: "Guest-id",
  },
};

axios(config)
  .then(function (response) {
    console.log(JSON.stringify(response.data));
  })
  .catch(function (error) {
    console.log(error);
  });
// This one is made with Postman API help.
```
Also, I would like to mention different layers of security for accessing the POST/GET a tweet. 


#### - Analyze this post. OpenAI.

OpenAI gives excellent documentation on how to use their powerful tools. I definitely should've divided this post. 

Register here and we can start a knowledgeable but straightforward tutorial on working with OpenAI code. [Link to the repo:](https://github.com/aserputov/openAI). 


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

This tutorial teaches us how GPT3 can create something depending on your question. Clever. SIRI itself.
In the tutorial, we learned how GPT3 could create different names for animals we provide.

Now we can ask it different custom questions. Questions from the Twitter post.
 

#### - Post request to OpenAi GPT3 with the information from this post.

My openAI + getPost are both made with Node.js. I just connected both of them into one code. [Link to the repo.](https://github.com/aserputov/openAItweet
)
We can successfully get a text from the tweet and transform it with with OpenAI. 

#### - Get response from OpenAi.

We have already received a response in our Node.js application.

#### - Post this response like a reply to the initial tweet.

How can we do that? The problem I am stuck on right now is how to send a reply from the node.js application into python code. I hope to finish this one as soon as possible. 



#### ∞ Get the reply and so on.


This blog post has been lying in my archives for too long now. I hope to finish this project in a few days.

If you have any questions or suggestions, please, I will be glad to hear them.

### Conclusion

> What a Time to be Alive? 

Bullion of gold: 
https://github.com/twitterdev/Twitter-API-v2-sample-code

⚠️ Previous Blog Post Tips And Tricks About Shell & CURL [Link]
This post is the best for short commands.

⚠️ Future Blog Post Will Be Posted As Soon As Possible: Network&Internet Architecture [Link]

### Links

🖇 Follow me on [GitHub](https://github.com/aserputov) 

🖇 Follow me on [Twitter](https://twitter.com/aserputov) 

_p.s This post was made out of my curiosity