Photo: Courtesy of Joshua Hoehne
Twitter is one of the most popular social media channels. The importance of Twitter is that it can be a serious communication tool with serious information sharing. Probably for that reason many businesses, websites, entrepreneurs, thought leaders, experts decide to be active on Twitter despite the noise and inappropriate usage that takes place.
It’s especially unique for how accessible it makes your target audience or leaders of your industry.
One of the challenges that social media managers face though, is that it can be really tiring and tedious to manage Twitter accounts. We have good news. Although there are already more professional paid alternatives, if you are into Python you too can develop something simple to automate your Twitter activity.
Prep (Shortcuts & Disclaimer)
So, to dive right into it, “n” is the shortcut key for new tweet in Twitter. Also, Ctrl+Enter is for sending the tweet. This makes it so easy. We can tweet by just using those 2 shortcut keys with SendKeys method.
There are many other shortcut keys in Twitter. They pretty much cover every single task you can carry out on the social media network.
Twitter is pretty strict with bots and inappropriate usage so make sure you are following their guidelines and user terms. This article is for educational purposes only. Please use responsibly.
For that reason you might want to load up Twitter first and make sure your connection is not restricted and/or solve any necessary Captcha questions in advance.
You can visit this Twitter page for more Twitter shortcuts.
Sending a Tweet on Twitter with Python (Sendkeys Method)
We need to get started with the necessary libraries. We’ll use:
- webbrowser: To launch default browser
- time: To create little delays between keys
- win32com.client: To establish shell
import webbrowser
import time
import win32com.client
Creating a connection to Windows shell.
shell = win32com.client.Dispatch("WScript.Shell")
Creating a connection to Windows shell.
Now it makes sense to create a variable which consists of the tweet body. Something like:
tweet = """Hello World @World #Coding #Python"""
Now, we’ll launch twitter.com and then create an adequate amount of delay. This delay is important to get right because if the key sequence gets messed up the Python script will fail.
webbrowser.open("https://twitter.com")
time.sleep(7)
And the moment of truth.
All we have to do is:
- send “n” key for new tweet.
- Send the tweet body.
- And then, send Ctrl+Enter.
Ctrl+Enter can be sent with this syntax: “^{ENTER}”. You can check out Microsoft’s Official Documentation regarding Sending Key rules here.
Again we’re making use of 1 second delays so that Windows and browser can register key strokes properly in the right sequence.
### New Tweet Window
shell.SendKeys("n", 0)
time.sleep(1)
### Typing the new tweet
shell.SendKeys(tweet, 0)
time.sleep(1)
### Sending the new tweet
shell.SendKeys("^{ENTER}", 0)
time.sleep(1)
Congratulations. Your first automated tweet is sent! Here is the full code:
import webbrowser
import time
import win32com.client
### Creating the Shell Connection for Sending Keys
shell = win32com.client.Dispatch("WScript.Shell")
tweet = """Hello World @World #Coding #Python"""
### Opening Twitter on Default Browser
webbrowser.open("https://twitter.com")
time.sleep(7)
### New Tweet Window
shell.SendKeys("n", 0)
time.sleep(1)
### Typing the new tweet
shell.SendKeys(tweet, 0)
time.sleep(1)
### Sending the new tweet
shell.SendKeys("^{ENTER}", 0)
time.sleep(1)
You can create an image attachment function with this simple code and placing it at the right order with something like this:
Please note that it’s missing the keys for image selection, you could create something to paste image paths with Ctrl+V.
attach = False
if attach == True:
shell.SendKeys("{TAB}", 0)
time.sleep(1)
shell.SendKeys("{ENTER}", 0)
time.sleep(1)
shell.SendKeys("{ENTER}", 0)
shell.SendKeys("^{ENTER}", 0)
You can also find the code in Holy Python Github Repository here.
Closing & Some Ideas (Attachment, scheduling, gui, multi-emails)
You can also create further delays or take advantage of task scheduling to send multiple tweets at a predefined frequency throughout the day.
Also, there is a Gmail variation of this code for sending emails from Gmail with Python with the same method.
Thanks for checking out this tutorial. We have more tutorials in many different categories here.
Please share this page if you find it useful.