{"componentChunkName":"component---src-templates-tag-js","path":"/tags/twitter/","result":{"data":{"site":{"siteMetadata":{"title":"LoginRadius Blog"}},"allMarkdownRemark":{"totalCount":2,"edges":[{"node":{"fields":{"slug":"/engineering/beginners-guide-to-tweepy/"},"html":"<p>As we all know, Twitter is one of the most preferred microblogging websites when it comes to putting your thoughts on the internet. Big organizations use this platform for advertising their product; government institutions even use it to provide prompt customer resolution; various groups use Twitter to run social awareness campaigns and media campaigns. Twitter has close to 330 million monthly active users worldwide, out of which 17 million hits are generated from India itself along with more than 1 billion downloads on Play Store. With all this in mind, Twitter allows access to Twitter API to developers to create some cool applications like bots, automation tools, etc. This Twitter API gives developers access to almost all of Twitter's functionalities like likes, retweets, tweets, etc. Tweepy, a python package, helps us in achieving all this.</p>\n<p>Tweepy is a python package that smoothly and transparently accesses Twitter's endpoints made available for the developers. Without Tweepy, the user would have to take care of various low-level details about HTTP requests, rate limiting, authentication, serialization, etc. Tweepy handles all this mess on behalf of the user making the application prone to errors.\nIn simple words, Tweepy is an open-source python package that provides a way for developers to communicate with the Twitter API. But keep in mind that Twitter levies a rate limit on the number of requests made to the Twitter API. To be precise, 900 requests/15 minutes are allowed; Twitter feeds anything above that an error.</p>\n<h3 id=\"installation\" style=\"position:relative;\"><a href=\"#installation\" aria-label=\"installation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Installation</h3>\n<p>Tweepy can be installed by using Python package manager <strong>pip</strong>. A simple demonstration can be seen below:\n<img src=\"/40dde8efec2ad55ac4676067a47623cc/installation.png\" alt=\"installation\" title=\"Installation\"></p>\n<p>Installation on Linux and macOS should follow similar steps as well.</p>\n<p>Twitter API uses OAuth for authentication, so initially, you need to apply for authentication credentials from Twitter. These authentication credentials basically consists of 4 components namely : <em>consumer</em>key, consumer<em>secret</em>key, access<em>token, access</em>token<em>secret</em> . These credentials from Twitter are used to instantiate the API. <strong>Each account gets a unique key, so don't reuse someone else's keys.</strong>\nFor getting those credentials from Twitter, apply for a developer account on the <a href=\"https://developer.twitter.com/en\">Twitter Developers</a> page.\n<img src=\"/5c15dfefc6853d899b15baf7a91ab241/twitterdev.png\" alt=\"twitterdev\" title=\"Twitter Dev Dashboard\">\nThis is what the account looks like. Here you will get detailed information about the total no. of requests made, your API credentials, and much more information. After creating an account, you need to create an app wherein you will be asked to name your app and a short description. You must be wondering what an app is?\nThe app is like a gateway that contains a set of permissions and keys used to access the Twitter API. An app is needed for accessing the Twitter API as a part of Twitter's OAuth authentication. After creating an app, generate new authentication tokens for authorization purposes.</p>\n<p><img src=\"/3e1ad6c1ae5d8e5ab84b112068ba87f1/keys.png\" alt=\"keys\"></p>\n<h3 id=\"getting-started-tweepy\" style=\"position:relative;\"><a href=\"#getting-started-tweepy\" aria-label=\"getting started tweepy permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting Started: Tweepy</h3>\n<p>There are four common basic steps in any Tweepy application.</p>\n<ol>\n<li>Importing tweepy package.</li>\n<li>Setting the authentication credentials.</li>\n<li>Instantiating the API.</li>\n<li>Creating API object.</li>\n</ol>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> tweepy</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># authenticating twitter api credentials</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">consumer_key = </span><span class=\"mtk8\">&#39;2OsNoPKOYCpxxxxxxxxxxxxxxxxxxxxxx&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">consumer_secret = </span><span class=\"mtk8\">&#39;Xw07uU51xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">access_token = </span><span class=\"mtk8\">&#39;24621057xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">access_token_secret = </span><span class=\"mtk8\">&#39;pXt5xxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># instantiating the api</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">auth = tweepy.OAuthHandler(consumer_key, consumer_secret)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">auth.set_access_token(access_token, access_token_secret)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\"># creating API object</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">api = tweepy.API(auth,</span><span class=\"mtk12\">wait_on_rate_limit</span><span class=\"mtk1\">=</span><span class=\"mtk4\">True</span><span class=\"mtk1\">,</span><span class=\"mtk12\">wait_on_rate_limit_notify</span><span class=\"mtk1\">=</span><span class=\"mtk4\">True</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">import</span><span class=\"mtk1\"> tabulate</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">user = api.get_user(</span><span class=\"mtk8\">&quot;Cristiano&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;User Details:&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(tabulate([[</span><span class=\"mtk8\">&quot;Name&quot;</span><span class=\"mtk1\">,</span><span class=\"mtk8\">&quot;Description&quot;</span><span class=\"mtk1\">,</span><span class=\"mtk8\">&quot;Location&quot;</span><span class=\"mtk1\">],[user.name,user.description,user.location]],</span><span class=\"mtk12\">headers</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;firstrow&quot;</span><span class=\"mtk1\">))</span></span></code></pre>\n<blockquote>\n<p>Note: Use your credentials in the hidden keys above._</p>\n</blockquote>\n<p>Objects created from the Tweepy. API class helps us access most of Twitter's available functionality like tweets, retweets, likes, etc. In the code snippet, we used the api.get_user method for getting information about a certain user on Twitter. Likewise, there can be several use cases of different methods(discussed below) made available by Twitter to developers. You can find the link for this code <a href=\"https://colab.research.google.com/drive/1dN02ioXElOQPOktIzNBACCncyrI2eiBR?usp=sharing\">here.</a></p>\n<p>Now we will see different methods provided by Twitter. The API methods have been divided into groups based on their functionality. The detailed guide for the API methods can be found at the official <a href=\"https://tweepy.readthedocs.io/en/latest/api.html\">API Reference</a> documentation. </p>\n<ul>\n<li>Timeline methods</li>\n<li>Status methods</li>\n<li>User methods</li>\n<li>Direct Message Methods</li>\n<li>Friendship Methods</li>\n<li>Favorite Methods</li>\n<li>Block Methods</li>\n<li>Search Methods</li>\n<li>Trends Methods</li>\n<li>Geo Methods</li>\n</ul>\n<h4 id=\"timeline-methods\" style=\"position:relative;\"><a href=\"#timeline-methods\" aria-label=\"timeline methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Timeline Methods</h4>\n<p>These methods handles the tweets, retweets, statuses on your/someone else's timeline as long as the account is public.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">tweets = api.home_timeline()</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> tweet </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> tweets:</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(</span><span class=\"mtk4\">f</span><span class=\"mtk8\">&quot;</span><span class=\"mtk4\">{</span><span class=\"mtk1\">tweet.user.name</span><span class=\"mtk4\">}</span><span class=\"mtk8\"> said </span><span class=\"mtk4\">{</span><span class=\"mtk1\">tweet.text</span><span class=\"mtk4\">}</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>api.home_timeline() is an API method that returns the 20 most recent tweets on the user's timeline.</p>\n<h4 id=\"status-methods\" style=\"position:relative;\"><a href=\"#status-methods\" aria-label=\"status methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Status Methods</h4>\n<p>These methods deal with creating, fetching tweets, retweeting tweets.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.update_status(</span><span class=\"mtk8\">&quot;Hello World!&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>api.update_status() is an API method used to create a tweet on the user's timeline. For each update request, it will check the user's recent tweets. If any duplication is found, the request will be blocked by Twitter as a user cannot post the same tweet more than once.</p>\n<h4 id=\"user-methods\" style=\"position:relative;\"><a href=\"#user-methods\" aria-label=\"user methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>User Methods</h4>\n<p>These methods help to find the user details using various paramaeter like name, location, description,friends, followers,etc. as long as the account is public. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">user = api.get_user(</span><span class=\"mtk8\">&quot;ISRO&quot;</span><span class=\"mtk1\">)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(user.name)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(user.decscription)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(user.followers)</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">print</span><span class=\"mtk1\">(user.location)</span></span></code></pre>\n<p>We have used get_user() previously to fetch user details of specific accounts.</p>\n<h4 id=\"friendship-methods\" style=\"position:relative;\"><a href=\"#friendship-methods\" aria-label=\"friendship methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Friendship Methods</h4>\n<p>These methods help the user to follow, unfollow certain accounts, list the accounts user follows, etc.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.create_friendship(</span><span class=\"mtk8\">&quot;elonmusk&quot;</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>create_friendship() will add @elonmusk to the list of accounts you follow.</p>\n<h4 id=\"favorite-methods\" style=\"position:relative;\"><a href=\"#favorite-methods\" aria-label=\"favorite methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Favorite Methods</h4>\n<p>Likes or unlikes(if already liked) the status specified in the ID parameter as the authenticating user.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.create_favorite(tweet.id)</span></span></code></pre>\n<p>create_favorite() will like a tweet based on the tweet id provided.</p>\n<h4 id=\"block-methods\" style=\"position:relative;\"><a href=\"#block-methods\" aria-label=\"block methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Block Methods</h4>\n<p>Used to block, unblock, list blocked accounts of the user.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.create_block(</span><span class=\"mtk11\">id</span><span class=\"mtk1\">/screen_name)</span></span></code></pre>\n<p>create<em>block() will block the specific user using the id/screen</em>name provided. </p>\n<h4 id=\"search-methods\" style=\"position:relative;\"><a href=\"#search-methods\" aria-label=\"search methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Search Methods</h4>\n<p>These methods help the user to search specific tweet based upon the search query and parameters provided. But not all tweets will be indexed or made available through the search methods.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk15\">for</span><span class=\"mtk1\"> tweets </span><span class=\"mtk4\">in</span><span class=\"mtk1\"> api.search(</span><span class=\"mtk12\">q</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;iphone&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">lang</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;en&quot;</span><span class=\"mtk1\">):</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">print</span><span class=\"mtk1\">(tweet.text)</span></span></code></pre>\n<p>search() will look out for all the tweets available for the query keyword 'q' provided.</p>\n<h4 id=\"trends-methods\" style=\"position:relative;\"><a href=\"#trends-methods\" aria-label=\"trends methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Trends Methods</h4>\n<p>It returns the trends going on at a specific geographical location.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.trends_place(</span><span class=\"mtk7\">1</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>trends_place() will show trends in the specific area. Here one stands for worldwide.</p>\n<h4 id=\"geo-methods\" style=\"position:relative;\"><a href=\"#geo-methods\" aria-label=\"geo methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Geo Methods</h4>\n<p>It returns the geographical location like latitude, the longitude of the place id provided.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"python\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">api.geo_id(</span><span class=\"mtk11\">id</span><span class=\"mtk1\">)</span></span></code></pre>\n<p>geo_id() returns more geographical information of the concerned place id.</p>\n<h3 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h3>\n<p>Tweepy allows the user to concentrate on the application's logic by hiding many low-level details, thus making the application bug-free. You can use tweepy to do some cool projects like bots, automation, machine learning applications, etc. </p>\n<h5 id=\"ket-takeaways\" style=\"position:relative;\"><a href=\"#ket-takeaways\" aria-label=\"ket takeaways permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Ket Takeaways</h5>\n<ul>\n<li>What is Tweepy?</li>\n<li>Installation</li>\n<li>Getting Started with Tweepy</li>\n<li>Various API methods</li>\n</ul>\n<p>More information about tweepy can be found at <a href=\"https://tweepy.readthedocs.io/en/latest/index.html\">docs.</a> Make sure to look at the official documentation as it will provide you with a greater picture of the package. So what are you waiting for? Go ahead, use your imagination, and get started with the side-project you've always been thinking of.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n</style>","frontmatter":{"date":"November 02, 2020","updated_date":null,"title":"Beginners Guide to Tweepy ","tags":["Twitter","Tweepy","Python"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/63d4d3ea1070111c43e0bdfa476b2a62/14b42/coverimage.jpg","srcSet":"/static/63d4d3ea1070111c43e0bdfa476b2a62/f836f/coverimage.jpg 200w,\n/static/63d4d3ea1070111c43e0bdfa476b2a62/2244e/coverimage.jpg 400w,\n/static/63d4d3ea1070111c43e0bdfa476b2a62/14b42/coverimage.jpg 800w,\n/static/63d4d3ea1070111c43e0bdfa476b2a62/47498/coverimage.jpg 1200w,\n/static/63d4d3ea1070111c43e0bdfa476b2a62/0e329/coverimage.jpg 1600w,\n/static/63d4d3ea1070111c43e0bdfa476b2a62/d8255/coverimage.jpg 1920w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Sameer Mahajan","github":"sameermahajan101","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/build-a-twitter-bot-using-nodejs/"},"html":"<h2 id=\"build-a-twitter-bot-with-nodejs\" style=\"position:relative;\"><a href=\"#build-a-twitter-bot-with-nodejs\" aria-label=\"build a twitter bot with nodejs permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Build a Twitter Bot with NodeJs</h2>\n<p>Hey there! In this blog post, we will build a twitter bot that let us tweet using the command line directly. </p>\n<blockquote>\n<p><a href=\"https://nodejs.org/en/download/\">Node</a> and <a href=\"https://www.npmjs.com/\">npm</a> (or, yarn) must be installed on your system. </p>\n</blockquote>\n<h2 id=\"lets-start-\" style=\"position:relative;\"><a href=\"#lets-start-\" aria-label=\"lets start  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Let's Start 🎉</h2>\n<p>We will use <strong><a href=\"https://github.com/desmondmorris/node-twitter\">twitter</a></strong> - a client library for the Twitter REST and Streaming APIs. This npm package will make the whole process of building the bot a whole lot easier and faster. </p>\n<h3 id=\"1-apply-for-a-twitter-developer-account\" style=\"position:relative;\"><a href=\"#1-apply-for-a-twitter-developer-account\" aria-label=\"1 apply for a twitter developer account permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Apply for a Twitter Developer Account</h3>\n<p>To be able to access Twitter API, you need a Twitter Developer Account. Apply for it <a href=\"https://developer.twitter.com/en/apply-for-access\">here</a>. Click on <em>Apply for a Developer Account</em>.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 33.07692307692307%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA/UlEQVQY03WPyW6DQAyGefCq5z5EH6BLLlGkKuXEpVEO5JKqt1aVWGYoCYQQwLPZU4NyDJ80lrffYwdJkp7q2hpjtNYKrDVKKQ6dtUrpYQA/MRYnAEBzsyP2gwHUWbtG+4uhk6bW+kZha/xgiTXkryASjlwtkXeIARe+8zKr+9+iTo7nH1n9XXRadYd26LsOQPl5Ah6yent/en5ZLF53u3i9Xn/u92EYRlG0XK4+Npvxf6JZcaVs0wMfOa2EzqHh06fQOU6PsH9LjFgWhRAyy0UupBSikDLPsmKC82maHY4Vz7ohRvKPX+p+Cw8x3G0hLm1nsAJiy0uNb2Zn5h9k5Y5fQoRJkwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Twitter Developer Account\"\n        title=\"Twitter Developer Account\"\n        src=\"/static/16a0b5bcb6a6661a39b0bb6f3fd230ec/e5715/twitter.png\"\n        srcset=\"/static/16a0b5bcb6a6661a39b0bb6f3fd230ec/a6d36/twitter.png 650w,\n/static/16a0b5bcb6a6661a39b0bb6f3fd230ec/e5715/twitter.png 768w,\n/static/16a0b5bcb6a6661a39b0bb6f3fd230ec/d8817/twitter.png 1238w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>It will ask you to log in. Select appropriate settings after logging in. It will also generate <strong>keys</strong> and <strong>secret tokens</strong> like Consumer Key, Consumer Secret, Access Token Key, Access Token Secret. Copy and save them somewhere for future use. </p>\n<h3 id=\"2-setup-project\" style=\"position:relative;\"><a href=\"#2-setup-project\" aria-label=\"2 setup project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Setup Project</h3>\n<p>Make a project folder with any name of your choice. Run <code>npm init</code> to initialize the project. This will generate a <code>package.json</code> file in your directory. Alternatively, you can run <code>npm init -y</code> to setup your project with default settings.</p>\n<blockquote>\n<p><code>package.json</code> holds all the dependencies of a project.</p>\n</blockquote>\n<p>Since our project will be dependent on <strong>twitter</strong> client library, it's time we install this dependency in our project. Run <code>npm install twitter</code> to include this in your <code>package.json</code> .</p>\n<h3 id=\"3-setup-files\" style=\"position:relative;\"><a href=\"#3-setup-files\" aria-label=\"3 setup files permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Setup Files</h3>\n<p>Now we will start with the actual coding part. At the moment, your project folder will have a <code>node_modules</code> folder, a <code>package.json</code> file, and a <code>package-lock.json</code> file. If you have gone with the default options while running <code>npm init</code>, then <code>index.js</code> is the file we will be working upon.</p>\n<p>Open <code>index.js</code>. To be able to use the <strong>twitter</strong> library, which we have installed, you need to include it in <code>index.js</code>. </p>\n<p><code>var Twitter = require('twitter');</code></p>\n<p>Also, remember the <strong>keys</strong> and <strong>tokens</strong> you got from Twitter Developer Portal? It's time we use them. </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">client</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">new</span><span class=\"mtk1\"> </span><span class=\"mtk10\">Twitter</span><span class=\"mtk1\">({</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t</span><span class=\"mtk12\">consumer_key:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">process</span><span class=\"mtk1\">.</span><span class=\"mtk12\">env</span><span class=\"mtk1\">.</span><span class=\"mtk12\">CONSUMER_KEY</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t</span><span class=\"mtk12\">consumer_secret:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">process</span><span class=\"mtk1\">.</span><span class=\"mtk12\">env</span><span class=\"mtk1\">.</span><span class=\"mtk12\">CONSUMER_SECRET</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t</span><span class=\"mtk12\">access_token_key:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">process</span><span class=\"mtk1\">.</span><span class=\"mtk12\">env</span><span class=\"mtk1\">.</span><span class=\"mtk12\">ACCESS_TOKEN_KEY</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t</span><span class=\"mtk12\">access_token_secret:</span><span class=\"mtk1\"> </span><span class=\"mtk12\">process</span><span class=\"mtk1\">.</span><span class=\"mtk12\">env</span><span class=\"mtk1\">.</span><span class=\"mtk12\">ACCESS_TOKEN_SECRET</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">});</span></span></code></pre>\n<p><strong>Do not add your Consumer or Access Token keys or secrets directly in the <code>index.js</code>.</strong> These should be managed accordingly using <em>environment variables</em>.</p>\n<blockquote>\n<p>We will use <code>dotenv</code> library to manage our environment variables in NodeJS.  Run <code>npm install dotenv</code> and follow <a href=\"https://www.npmjs.com/package/dotenv#usage\">these</a> instructions. Your <code>.env</code> will look something like this.</p>\n</blockquote>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">CONSUMER_KEY</span><span class=\"mtk1\">=........................................</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">CONSUMER_SECRET</span><span class=\"mtk1\">=.....................................</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">ACCESS_TOKEN_KEY</span><span class=\"mtk1\">=....................................</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">ACCESS_TOKEN_SECRET</span><span class=\"mtk1\">=.................................</span></span></code></pre>\n<h3 id=\"4-start-coding-\" style=\"position:relative;\"><a href=\"#4-start-coding-\" aria-label=\"4 start coding  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. Start Coding 🎯</h3>\n<p>I will use <a href=\"https://github.com/15Dkatz/official_joke_api\">Official Joke API</a> to automatically fetch content for my tweet. You can visit the repository and learn more about the endpoints. But the only <a href=\"https://github.com/15Dkatz/official_joke_api#grab-a-random-joke\">endpoint</a> we will work with is <a href=\"https://official-joke-api.appspot.com/random_joke\">random_joke</a>. Click on this, and you will receive a JSON response with fields id, type, setup, and punchline. The fields we are concerned with are setup and punchline. </p>\n<p>We will use <a href=\"https://github.com/axios/axios\">axios</a> for calling the API endpoint. Run <code>npm install axios</code> and add <code>var axios = require('axios');</code> in <code>index.js</code>.</p>\n<p>Now everything has been laid out. We need to connect the dots. The Official Joke API will return a JSON response. We will grab the required data, i.e., the setup and the punchline from it. And then make use of <a href=\"https://github.com/desmondmorris/node-twitter/tree/master/examples#tweet\">client.post</a> functionality of <strong>twitter</strong> package.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">axios</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;https://official-joke-api.appspot.com/random_joke&quot;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">then</span><span class=\"mtk1\">(</span><span class=\"mtk12\">Response</span><span class=\"mtk1\"> </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t</span><span class=\"mtk15\">return</span><span class=\"mtk1\"> [</span><span class=\"mtk12\">Response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">data</span><span class=\"mtk1\">.</span><span class=\"mtk12\">setup</span><span class=\"mtk1\">, </span><span class=\"mtk12\">Response</span><span class=\"mtk1\">.</span><span class=\"mtk12\">data</span><span class=\"mtk1\">.</span><span class=\"mtk12\">punchline</span><span class=\"mtk1\">];</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}).</span><span class=\"mtk11\">then</span><span class=\"mtk1\">(([</span><span class=\"mtk12\">setup</span><span class=\"mtk1\">, </span><span class=\"mtk12\">punch</span><span class=\"mtk1\">]) </span><span class=\"mtk4\">=&gt;</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t</span><span class=\"mtk12\">client</span><span class=\"mtk1\">.</span><span class=\"mtk11\">post</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&#39;statuses/update&#39;</span><span class=\"mtk1\">, {</span><span class=\"mtk12\">status:</span><span class=\"mtk1\"> </span><span class=\"mtk8\">`</span><span class=\"mtk4\">${</span><span class=\"mtk12\">setup</span><span class=\"mtk4\">}</span><span class=\"mtk6\">\\n</span><span class=\"mtk4\">${</span><span class=\"mtk12\">punch</span><span class=\"mtk4\">}</span><span class=\"mtk8\">`</span><span class=\"mtk1\">}, </span><span class=\"mtk4\">function</span><span class=\"mtk1\">(</span><span class=\"mtk12\">error</span><span class=\"mtk1\">, </span><span class=\"mtk12\">tweet</span><span class=\"mtk1\">, </span><span class=\"mtk12\">response</span><span class=\"mtk1\">){</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t\t</span><span class=\"mtk15\">if</span><span class=\"mtk1\">(!</span><span class=\"mtk12\">error</span><span class=\"mtk1\">){</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t\t\t</span><span class=\"mtk10\">console</span><span class=\"mtk1\">.</span><span class=\"mtk11\">log</span><span class=\"mtk1\">(</span><span class=\"mtk12\">tweet</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t\t}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">\t})</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">});</span></span></code></pre>\n<p>Now run <code>node index.js</code> on the terminal and see the output.\nIf everything went well, you would see a <em>tweet</em> response, otherwise an <em>error</em> message. The successful message would look something like <a href=\"https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-update#example-response\">this</a>.</p>\n<p>At this point, your index.js should look like this.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 45.69230769230769%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABBklEQVQoz42R627DIAyFeZAmGBsMJCHc2iRqs/3p+7/TSPdjUzdVPbLE4fLJ1kGElGIuOQznOl/aYhmklH0v35BAdsaNho3xji0rUF1/sF136k6n/qUEsS/7Pa4f03IN2x7qapdP3u46rcjDr5fyP9jwULaxbkNZ3VybcelM5UZlx3IFN7VB2ghHdd0zbIwmBeYoqRUwKkvKYjNgCa0mrdEwEztQ+NRfjKO/5FLOS12uuV5SyjGmmEqYY8y1GWtZG3PAiA/2hxftYkolL1sqdZ6myXtCRdYDanJjY4wPCnXbIntiq63X7HgImllIAAXQjtgPRxPrHz/1lgRpAoDvPI5I/qTyQl8o80v3xwnZmgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"index.js\"\n        title=\"index.js\"\n        src=\"/static/0758fb1dcb8133462eba5090310a8030/e5715/code.png\"\n        srcset=\"/static/0758fb1dcb8133462eba5090310a8030/a6d36/code.png 650w,\n/static/0758fb1dcb8133462eba5090310a8030/e5715/code.png 768w,\n/static/0758fb1dcb8133462eba5090310a8030/c3fd4/code.png 854w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<h3 id=\"congratulations-\" style=\"position:relative;\"><a href=\"#congratulations-\" aria-label=\"congratulations  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Congratulations! 🎊</h3>\n<p>That's it! You have created your first Twitter Bot, which tweets content after directly calling it from an API. You might take a look at the <a href=\"https://github.com/LoginRadius/engineering-blog-samples/tree/master/NodeJs/TwitterBot\">repository</a> if you encountered any errors. Here, I have used different APIs to fetch content and tweet. Moreover, I have added a script that will search posts with a particular hashtag and like them. </p>\n<blockquote>\n<p><strong>While pushing your code to GitHub, make sure to add <code>.gitignore</code> file and include <code>node_modules</code> and <code>.env</code> in it.</strong></p>\n</blockquote>\n<h2 id=\"further-reading\" style=\"position:relative;\"><a href=\"#further-reading\" aria-label=\"further reading permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Further Reading</h2>\n<blockquote>\n<p>The world is yours to explore.</p>\n</blockquote>\n<p>And the dev world is based entirely on exploring technologies and frameworks. Play with this <strong>twitter</strong> library and see what interesting stuff can be built with it. Maybe, search tweets with a particular hashtag and like them? Or maybe, retweet tweets of any particular user? You are free to use your imagination and coding skills to explore more and more. If <code>axios</code> and <code>dotenv</code> are new to you, then go ahead and read their documentation. Get hold of the concepts of environment variables. And come up with your own cool Twitter Bot.</p>\n<blockquote>\n<p>Keep in mind Twitter's <a href=\"https://developer.twitter.com/en/developer-terms\">Developer Policy &#x26; Terms</a> and <a href=\"https://twitter.com/en/tos\">Twitter Terms &#x26; Conditions</a>. These should not be violated in whatever project you are building.</p>\n</blockquote>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n</style>","frontmatter":{"date":"October 14, 2020","updated_date":null,"title":"Build A Twitter Bot Using NodeJS","tags":["NodeJs","Twitter","Bot"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/73b57d06f8809fab5ebb6ad89ab328ef/14b42/cover.jpg","srcSet":"/static/73b57d06f8809fab5ebb6ad89ab328ef/f836f/cover.jpg 200w,\n/static/73b57d06f8809fab5ebb6ad89ab328ef/2244e/cover.jpg 400w,\n/static/73b57d06f8809fab5ebb6ad89ab328ef/14b42/cover.jpg 800w,\n/static/73b57d06f8809fab5ebb6ad89ab328ef/47498/cover.jpg 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Vineeta Jain","github":"Ninjavin","avatar":null}}}}]}},"pageContext":{"tag":"Twitter"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}