Startups and agencies trying to promote their business via Twitter Marketing often struggle with getting themselves seen. Getting your target audience and gaining enough interest and engagement may sometimes seem far-fetched.
Social Media Marketing (by Canva)
Added to this, there is the manual work wherein the right audience need to be identified and DMs (Direct Messages) sent to 1000s of Twitter users one after the other. What if there’s a better way to reduce some of the manual workload? If there was a way to automate DMs being sent to a list of audience, wouldn’t that be great for your marketing campaigns? Besides, this solution will also be useful to Social Media Influencers enabling them to share targeted posts and messages to their followers in a timely fashion.
What you will need
- Python 3 and above (If you are a newbie to Python, check out this course-Programming for Everybody (Getting Started with Python) | Coursera)
- A python notebook of your choice (My preference is towards Jupyter and Visual Studio Code (VSCode).
- Twitter API- (Check this out to learn more — Twitter API Documentation | Docs | Twitter Developer Platform)
Schematic — Sending Direct Messages to specific users/Twitter Marketing
Twitter provides an API that is packed with pretty useful functions. Python provides 2 very useful packages that will be helpful in this process — Tweepy and Twython. Both libraries have a variety of functions for processing Twitter messages. They complement each other. In this code, specific functions from both libraries have been utilized.
Setting up the Authentication
As you might know, the Twitter API has a 15-minute rate limit. Read more below. Each function has a rate limit of its own.
As per the above URL, it becomes clear that the rate limit is determined by the end point. As per the use case in hand, we are required to look up the user’s followed list –
Let’s say that you are looking to retrieve followers of a user ‘XYZ’. Table 1.0 shows what is being done as per the code snippet –
The ‘time’ library is useful to tackle rate limits wherein you can have the API sleep after the rate window and then resume for the next rate window.
The final part — Sending Direct Messages to the followers –
You can definitely wrap this up in a function and pass the recipient user ids as a list.
Potential Limitations and Recommendations
The Twitter API v1.0 provides a way to determine whether a recipient has enabled DMs or not. However, the Twitter API v1.0 had this feature. The below URLs provide more information.
One solution to this would be to leverage the API v.10 to check whether a recipient has enabled DMs or not. Below is a URL –
The above method can greatly reduce the time taken to promote your marketing campaigns. You can determine your target audience and effectively improve engagement for your product/service. If you are a social media influencer, you can use this to promote yourself as a brand as well.