CodeNewbie Community 🌱

Cover image for How to Send a Tweet with GitHub Copilot
Rizel Scarlett for GitHub

Posted on

How to Send a Tweet with GitHub Copilot

This month, I learned how to send a tweet with GitHub Copilot. It was so cool! I'm anticipating that you may wonder:

  • What is GitHub Copilot?
  • Why did I want to send a tweet with GitHub Copilot?
  • What's the value in it for you?
  • And how did I send a tweet with Copilot?

Fortunately, I'll answer all of those questions in this post!

Firstly, what is GitHub Copilot?

Copilot is an AI pair programmer that helps you write code faster with less work. If you have never used it and you're struggling to envision the behavior, I would liken it to the Smart Compose feature in Gmail and Google Docs. Similar to Smart Compose, GitHub Copilot anticipates your next line of code to boost your productivity.

An email that says 'Hey Jacqueline. Does next' and Smart Compose suggests the following words: 'Tuesday work for you?'

Why am I using GitHub Copilot to send tweets?

Reason 1

I'm a developer advocate, and part of my role is building demos to increase developer awareness of new features. In July, Twitter's DevRel team asked the GitHub DevRel team to collaborate on a stream showing Twitter API v2 and GitHub Copilot. The point of the stream is to show that GitHub Copilot can help you leverage Twitter's API v2. I excitedly agreed to do the stream, and then I realized I had to use Tweepy, an easy-to-use Python library for accessing the Twitter API. I had one huge, glaring problem: I don't know how to write in Python, so it's not easy for me to use. I'm a trained JavaScript developer. I can even find my way around a Java or C# codebase, but I never touched Python in my life. Everyone says Python is easy because it's easy to read, and that's the language they learned in school. However, I had less than one week to prepare and deliver the demo with strangers on the internet watching me.

Coincidentally, I found myself leaning on Copilot to access Twitter's API with Tweepy. Although I was nervous, I realized this was a perfect situation. I could organically show how GitHub Copilot helped me to use Tweepy. And it worked! GitHub Copilot helped me build the demo in less than 30 minutes. Although I had a few hiccups during the stream, it was effective.

Admittedly, in my role, I've signed up to demonstrate, discuss, or build with unfamiliar tools because I believe that leaving my comfort zone will help me to grow and learn new concepts quickly. I've used GitHub Copilot for multiple projects to help me complete quick projects. A few examples include:
Demonstrating GitHub Copilot with Applitools.
Building automation in my repositories.
Just completing random short applications in less than a day.

GitHub Copilot is not something that replaces learning the intricacies of how to code because without knowing what you're doing, you're prone to inadvertently introducing bugs or poor practices. However, if you have an idea of what you want to accomplish and a solid understanding of the logic behind the lines of code you want to write, but you're not familiar with the syntax, Copilot can help.

Reason 2

I chose to send a tweet with Copilot because I'm addicted to Twitter, and I felt programmatically sending a tweet is more exciting than retrieving tweets via a GET request.

Reason 3

At face value, programmatically sending a tweet doesn't seem practical. One idea I had is that open source projects could automagically tweet Good First Issues whenever an issue is labeled 'good first issue.' The purpose is to attract new contributors and help new contributors find achievable issues. With the help of Copilot, I built out a solution for this using GitHub Actions, which I will write about in a future post.

What is the value in it for you?

If you're curious about how GitHub Copilot can work for you, I wrote about its various use cases here. For my short answer, it's fun, and you will learn how to optimize Copilot's features to boost your productivity.

How to send a tweet with Copilot

Step 1: Sign up for a Twitter Developer Account

You can find the information on signing up for a Twitter Developer Account in Twitter's documentation.

Step 2: Create an App on the Twitter Developer Platform

This step will help you generate the necessary tokens and keys to access Twitter's API. Please note: this is not creating an application that exists on an app store. Instead, this refers to the "app" you're building to interact with APIs with Twitter's API. Save the consumer secret, consumer key, access token, and access token secret because we will use those credentials later in this tutorial.

Step 3: Enable GitHub Copilot

To enable GitHub Copilot, navigate to https://github.com/settings/copilot. This should bring you to a page with a button that prompts you to enable GitHub Copilot. Please note that you have free access to GitHub Copilot if you're a student.

Page that prompts developers to enable GitHub Copilot with green button

Step 4: Install the Copilot extension in your editor

You can install the GitHub Copilot extension in Neovim, JetBrains IDEs, Visual Studio, and Visual Studio Code. In the picture below, I've highlighted the GitHub Copilot extension and GitHub Copilot Labs extension. Read my past blog post to learn about the differences between these extensions. Once you install the extension, it may ask you to sign in, so follow the prompts to complete the authentication process.

Highlights the Copilot Labs extension and the Copilot extension

Step 5: Install Tweepy

As I mentioned, tweepy is a Python library that makes it easy to use the Twitter API. Twitter seems to appreciate its existence because the Twitter DevRel team suggested I use that library. To get started with Tweepy,

  • Create a folder where you want this project's related code to reside.
  • Inside that folder, run the following command to install tweepy:
pip3 install tweepy
Enter fullscreen mode Exit fullscreen mode
  • If you've previously installed tweepy, run
pip3 install tweepy --upgrade
Enter fullscreen mode Exit fullscreen mode

Step 6: Create a file called sendTweet.py

I stored the file sendTweet.py in a new directory (aka folder) called copilot-tweepy.

Blank file that named sendTweet.py

Step 7: Let’s use Copilot to import the tweepy library

I wrote a comment that said

# import the tweepy library
Enter fullscreen mode Exit fullscreen mode

This was helpful for me because I was unsure how you handle library imports in Python, and Copilot showed me how. In the screenshot below, it populates a line of code that says,

import tweepy
Enter fullscreen mode Exit fullscreen mode

my comment that says import the tweepy library and Copilot suggesting I import Tweepy

Step 8: View multiple suggestions from Copilot

I wrote a comment so I could use the tweepy client and my API credentials, but Copilot didn't suggest the desired solution! Because the Twitter API v2 is still relatively new, it will not appear as the top suggestion.

The screenshot below shows Copilot making a suggestion that's inaccurate for my goals.

Copilot making an inaccurate suggestion

By default, Copilot will provide an initial suggestion, but you can see more of Copilot's suggestions by:

  • Hovering over the Copilot suggestion and it will display options to see more suggestions

    OR

  • use keyboard shortcuts. Learn about Copilot keyboard shortcuts per each specific operating system here.

For this situation, I chose the 'Open GitHub Copilot' option to show multiple suggestions. It opened a new tab with 119 lines of suggestions. After reading a bit of the Twitter API documentation, I learned that tweepy.Client is the correct syntax for Twitter API v2, so I searched specifically for the keywords tweepy.Client. I found the solution I needed and accepted that solution.

Image description

The suggestion that worked best for me was

client = tweepy.Client(consumer_key="",
                       consumer_secret="",
                       access_token="",
                       access_token_secret="")
Enter fullscreen mode Exit fullscreen mode

Step 9: Create a configuration file to store your credentials

I created a file called config.py in the same directory as sendTweet.py. I used this file to avoid exposing API credentials to the public.

Step 10: Use Copilot to autocomplete the config file

I wrote these first few lines in my configuration file.

# consumer secret
CONSUMER_SECRET_KEY = ""
# consumer key
Enter fullscreen mode Exit fullscreen mode

Copilot can now infer that I need to include an ACCESS_TOKEN and ACCESS_TOKEN_SECRET.

Image description

I passed in the correct values for each key.

Step 11: In the sendTweet.py file, import the configuration file with Copilot

I wrote a comment that said # import the config file and accepted Copilot's suggestion on the next line.

Image description

Step 12: In the sendTweet.py file, pass in the values for the following parameters: consumer_key, consumer_secret, access_token, and access_token_secret.

As you fill in each value, Copilot will notice the pattern and autofill the values for you!

Image description

Here’s what that block of code should look like when you’ve entered all the values:

client = tweepy.Client(consumer_key=config.CONSUMER_KEY,
                       consumer_secret=config.CONSUMER_SECRET_KEY,
                       access_token=config.ACCESS_TOKEN,
                       access_token_secret=config.ACCESS_TOKEN_SECRET)
Enter fullscreen mode Exit fullscreen mode

Step 13: In the sendTweet.py file, write the contents of the tweet

I wrote a comment that said,

# create a  variable called tweet with a string that says 'I wrote this tweet with Copilot
Enter fullscreen mode Exit fullscreen mode

Copilot correctly interpreted my comment, so I accepted the initial suggestion.

Image description

Step 14: Now, it’s time to send the tweet

In this step, I had to write a very specific comment. I initially wrote a comment that said,

# send tweet
Enter fullscreen mode Exit fullscreen mode

Unfortunately, Copilot spit out suggestions from Twitter API v1. To prompt Copilot to provide the desired suggestion, I wrote

# send tweet with client.create_tweet method and text as argument. Save the result in a variable called response
Enter fullscreen mode Exit fullscreen mode

This time Copilot gave me the correct results.

Image description

The line that sends the tweet should like this:

# send tweet with client.create_tweet method and text as argument. Save the result in a variable called response
response = client.create_tweet(text=tweet)
Enter fullscreen mode Exit fullscreen mode

Step 15: Print the value of the response to sanity check.

Printing the response variable may help us learn if our tweet was successful or not and why.

print(response)
Enter fullscreen mode Exit fullscreen mode

Step 16: Confirm that both files have all the required lines of code

Both my files looked like the code snippets below:

sendTweet.py

# import the tweepy library
import tweepy
# import the config file
import config
# create client with tweepy.Client with consumer key and consumer secret and access token as arguments
client = tweepy.Client(consumer_key=config.CONSUMER_KEY,
                       consumer_secret=config.CONSUMER_SECRET_KEY,
                       access_token=config.ACCESS_TOKEN,
                       access_token_secret=config.ACCESS_TOKEN_SECRET)

# create a  variable called tweet with a string that says 'I wrote this tweet with Copilot'
tweet = 'I wrote this tweet with Copilot'

# send tweet with client.create_tweet method and text as argument. Save the result in a variable called response
response = client.create_tweet(text=tweet)


print(response)
Enter fullscreen mode Exit fullscreen mode

config.py

# consumer secret 
CONSUMER_SECRET_KEY = "YOUR SECRET KEY GOES HERE"
# consumer key
CONSUMER_KEY = "YOUR KEY GOES HERE"
# access token
ACCESS_TOKEN = "YOUR TOKEN GOES HERE"
# access token secret
ACCESS_TOKEN_SECRET = "YOUR TOKEN SECRET GOES HERE"
Enter fullscreen mode Exit fullscreen mode

Step 17: Run the code

In my terminal, I ran the commands: python3 sendTweet.py. To check that it was successful, I headed to my Twitter feed and found the tweet!

Image description

Lessons Learned

The key to getting the best results from Copilot is:

  • writing clear, specific comments
  • leveraging Copilot once it can recognize continuous patterns in your code
  • and checking for alternate suggestions from Copilot

I hope you had fun experimenting with Copilot and the Twitter API v2! If this was successful for you, link your tweet in the comments below!

Latest comments (0)