DEV Community

Cover image for Collaboration Of Twitter&OpenAI API
A Serputov
A Serputov

Posted on

Collaboration Of Twitter&OpenAI API

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

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. I thought it would be great to do the same thing but on Instagram Reels. As always, how? I started searching for Instagram API:

We also can use this great tool: RapidAPI.

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!

Our step 0 is to get all the necessary keys.

Image description

Register here:(and get all your secrets)

Secrets:
Image description

- 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

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

//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.
Enter fullscreen mode Exit fullscreen mode

But luckily it worked:
Image description

- Get this tweet.

It's funny, but that part was much more straightforward. Code, here.

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.
Enter fullscreen mode Exit fullscreen mode

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:.

Image description

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.
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

🖇 Follow me on Twitter

_p.s This post was made out of my curiosity

Top comments (0)