Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Learn how to build your own Twitter bot with Python

#1
Learn how to build your own Twitter bot with Python

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/learn-how-to-build-your-own-twitter-bot-with-python.png" width="1238" height="890" title="" alt="" /></div><div><p>Twitter allows one to <a href="https://twitter.com">share</a> blog posts and articles with the world. Using Python and the <em>tweepy</em> library makes it easy to create a Twitter bot that takes care of all the tweeting for you. This article shows you how to build such a bot. Hopefully you can take the concepts here and apply them to other projects that use online services.</p>
<p><span id="more-21991"></span></p>
<h3>Getting started</h3>
<p>To create a Twitter bot the <a href="https://tweepy.readthedocs.io/en/v3.5.0/"><em>tweepy</em></a> library comes handy. It manages the Twitter API calls and provides a simple interface.</p>
<p>The following commands use <em>Pipenv</em> to install <em>tweepy</em> into a virtual environment. If you don’t have Pipenv installed, check out our previous article, <a href="https://fedoramagazine.org/install-pipenv-fedora/">How to install Pipenv on Fedora</a>.</p>
<pre>$ mkdir twitterbot $ cd twitterbot $ pipenv --three $ pipenv install tweepy $ pipenv shell</pre>
<h3>Tweepy – Getting started</h3>
<p>To use the Twitter API the bot needs to authenticate against Twitter. For that, <em>tweepy</em> uses the OAuth authentication standard. You can get credentials by creating a new application at <a href="https://apps.twitter.com/">https://apps.twitter.com/</a>.</p>
<h4>Create a new Twitter application</h4>
<p>After you fill in the following form and click on the <em>Create your Twitter application</em> button, you have access to the application credentials. <em>Tweepy</em> requires the <em>Consumer Key (API Key)</em> and the <em>Consumer Secret (API Secret)</em>, both available from the <em>Keys and Access Tokens.</em></p>
<p><img class="aligncenter size-full wp-image-22019" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/07/learn-how-to-build-your-own-twitter-bot-with-python.png" alt="" width="1238" height="890" /></p>
<p>After scrolling down the page, generate an <em>Access Token</em> and an <em>Access Token Secret </em>using the <em>Create my access token</em> button<em>.</em></p>
<h4>Using Tweepy – print your timeline</h4>
<p>Now that you have all the credentials needed, open a new file and write the following Python code.</p>
<div>
<pre>import tweepy auth = tweepy.OAuthHandler("your_consumer_key", "your_consumer_key_secret") auth.set_access_token("your_access_token", "your_access_token_secret") api = tweepy.API(auth) public_tweets = api.home_timeline() for tweet in public_tweets: print(tweet.text)</pre>
<p>After making sure that you are using the Pipenv virtual environment, run your program.</p>
</div>
<div></div>
<pre>$ python tweet.py</pre>
<p>The above program calls the <em>home_timeline </em> API method to retrieve the 20 most recent tweets from your timeline. Now that the bot is able to use <em>tweepy </em> to get data from Twitter, try changing the code to send a tweet.</p>
<h4>Using Tweepy – send a tweet</h4>
<p>To send a tweet, the API method <em>update_status</em> comes in handy. The usage is simple:</p>
<pre>api.update_status("The awesome text you would like to tweet")</pre>
<p>The <em>tweepy </em>library has many other methods that can be useful for a Twitter bot. For the full details of the API, check the <a href="http://docs.tweepy.org/en/v3.5.0/api.html#id1">documentation</a>.</p>
<h3>A magazine bot</h3>
<p>Let’s create a bot that searches for Fedora Magazine tweets and automatically retweets them.</p>
<p>To avoid retweeting the same tweet multiple times, the bot stores the tweet ID of the last retweet. Two helper functions, <em>store_last_id</em> and <em>get_last_id,</em> will be used to save and retrieve this ID.</p>
<p>Then the bot uses the <em>tweepy </em>search API to find the Fedora Magazine tweets that are more recent than the stored ID.</p>
<pre>import tweepy def store_last_id(tweet_id): """ Store a tweet id in a file """ with open("lastid", "w") as fp: fp.write(str(tweet_id)) def get_last_id(): """ Read the last retweeted id from a file """ with open("lastid", "r") as fp: return fp.read() if __name__ == '__main__': auth = tweepy.OAuthHandler("your_consumer_key", "your_consumer_key_secret") auth.set_access_token("your_access_token", "your_access_token_secret") api = tweepy.API(auth) try: last_id = get_last_id() except FileNotFoundError: print("No retweet yet") last_id = None for tweet in tweepy.Cursor(api.search, q="fedoramagazine.org", since_id=last_id).items(): if tweet.user.name == 'Fedora Project': store_last_id(tweet.id) tweet.retweet() print(f'"{tweet.text}" was retweeted'</pre>
<p>In order to retweet only tweets from the Fedora Magazine, the bot searches for tweets that contain <em>fedoramagazine.org</em> and are published by the “Fedora Project” Twitter account.</p>
<h3>Conclusion</h3>
<p>In this article you saw how  to create a Twitter application using the <em>tweepy</em> Python library to automate reading, sending and searching tweets. You can now use your creativity to create a Twitter bot of your own.</p>
<p>The source code of the example in this article is available on <a href="https://github.com/cverna/magabot">Github</a>.</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016