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.
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.
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.
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
- If you've previously installed tweepy, run
pip3 install tweepy --upgrade
Step 6: Create a file called sendTweet.py
I stored the file sendTweet.py in a new directory (aka folder) called copilot-tweepy
.
Step 7: Let’s use Copilot to import the tweepy library
I wrote a comment that said
# import the tweepy library
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
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.
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.
The suggestion that worked best for me was
client = tweepy.Client(consumer_key="",
consumer_secret="",
access_token="",
access_token_secret="")
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
Copilot can now infer that I need to include an ACCESS_TOKEN
and ACCESS_TOKEN_SECRET
.
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.
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!
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)
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
Copilot correctly interpreted my comment, so I accepted the initial suggestion.
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
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
This time Copilot gave me the correct results.
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)
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)
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)
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"
Step 17: Run the code
In my terminal, I ran the command: python3 sendTweet.py
. To check that it was successful, I headed to my Twitter feed and found the tweet!
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!
Top comments (23)
It is more like how to Tweet with python. But i get a point, Copilot can be really helpful, but needs to take some time to be more mature. (sometime is messsing with internal code completition like with Jetbrains PHPStorm) But overall I like it (i have subscription).
Anyway, nice tutorial and I going to try it right now because i finally have Twitter developer account ;)
The more we use it, the more it will mature! Let me know how it goes for you! I'm glad you like it overall.
Yeah, my purpose is to show how you can use Copilot to accomplish programming tasks. And this is based off a Twitch Stream I did with Twitter's DevRel team and they named it using Twitter API v2 with GitHub Copilot, so I'm not too far off here. 😊
copilot is awesome! It also can translate stuff.
oh wow, I love this example!!!
Hi Michael! I sent you a message on Twitter in your DMs. I'm writing a larger blog post around interesting use cases, and I would love to point to your use case. More details are included in the DM
Wow 👏
Yeah loved Github Copilot while it was free.
Have you tried Codewhisper ?
I haven't tried it out yet, but I've been meaning to! Similar to Copilot, after Code Whisperer is out of preview..they may start charging for it. But I do love that this is becoming a trend is software development!
Awesomee ! 👏👏
Thank you!!
Coming all the way to see how to tweet using github copilot then python ,but this is great content around really worth my time ,huge fan of copilot thanks for sharing.
Thank you!!!
Tabnine offers a very similar product that's been around considerably longer. I would suggest checking it out. It also doesn't suffer from the dubious legality issues that copilot suffers from.
I also like tabine!
Around the legality: I'm not a lawyer, but GitHub has developed and continue to evolve technical features to ensure Copilot remains compliant with legal requirements applicable to machine learning.
Cue post deletion by GitHub devrel in 5... 4... 3...
And my bad..I hid your comment because I get nervous about links. I thought it could be a spam link. I'll unhide it if it's not spam. I hid another link before yours. I clicked it and it sent me to some odd site that had nothing to do with copilot or coding, so I got nervous when I saw your link afterwards
This worked for me, thanks!
[twitter.com/digimodels/status/1612...]
I had to recreate the initial access token because it was read only. After regenerating the tokens and plugging them into VS Code, the function worked.
Tried doing this but the output was 'Forbidden'. I think we need Elevated access for this.
Very cool! Love it!
Thank you!!!
Some comments have been hidden by the post's author - find out more