├── .gitignore ├── last_seen_id.txt ├── keys_format.py ├── LICENSE ├── README.md └── my_twitter_bot.py /.gitignore: -------------------------------------------------------------------------------- 1 | keys.py 2 | __pycache__ 3 | -------------------------------------------------------------------------------- /last_seen_id.txt: -------------------------------------------------------------------------------- 1 | 1060682754885648389 -------------------------------------------------------------------------------- /keys_format.py: -------------------------------------------------------------------------------- 1 | # keys_format.py shares the same format as keys.py. 2 | # This file is not meant to be used in the main script. 3 | # Instead, copy this file into keys.py and put your own 4 | # keys there. 5 | CONSUMER_KEY = 'AAAAAA' 6 | CONSUMER_SECRET = 'BBBBBB' 7 | ACCESS_KEY = 'CCCCCC' 8 | ACCESS_SECRET = 'DDDDDD' 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Yosuke Kyle Sugi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Twitter Bot Sample 2 | A sample Twitter bot in Python - created by [@ykdojo](https://github.com/ykdojo). This is part of the [Edit Dojo](https://github.com/ykdojo/editdojo) project. 3 | 4 | --- 5 | 6 | ## Set up notes 7 | 8 | ### How to install Tweepy 9 | 10 | First, check your Python version with ``python3 --version`` or ``python --version`` on console (terminal/shell/command prompt). 11 | 12 | #### If you don't have Python 3 installed (if the above command fails): 13 | 14 | Either install Python 3 on your computer OR use something like PythonAnywhere (https://csdojo.io/py). 15 | 16 | #### If you have Python 3.6, you can just run: 17 | 18 | ``pip3 install tweepy`` 19 | 20 | #### If you have Python 3.7, run the following instead: 21 | 22 | ``pip3 install -U git+https://github.com/tweepy/tweepy.git@2efe385fc69385b57733f747ee62e6be12a1338b`` 23 | 24 | If the above command doesn't work, try replacing ``pip3`` with ``pip`` also. 25 | 26 | #### If you have Python 3.7 and want to use pipenv, use: 27 | 28 | ``pipenv install -e git+https://github.com/tweepy/tweepy.git@2efe385fc69385b57733f747ee62e6be12a1338b#egg=tweepy`` 29 | 30 | --- 31 | 32 | ## Files 33 | - **my_twitter_bot.py** - This is the main file that includes all the logic. 34 | - **last_seen_id.txt** - This will contain the ID of the tweet that my_twitter_bot.py has seen last. If you see any errors when running the main file, try replacing the content with the ID of one of the tweets you want to examine. 35 | - **keys_format.py** - This file is not meant to be used directly. Instead, copy this file in the same folder and rename it to keys.py. Then, put your Twitter API keys in keys.py. That way, my_twitter_bot.py will be able to use this information. 36 | -------------------------------------------------------------------------------- /my_twitter_bot.py: -------------------------------------------------------------------------------- 1 | import tweepy 2 | import time 3 | # NOTE: I put my keys in the keys.py to separate them 4 | # from this main file. 5 | # Please refer to keys_format.py to see the format. 6 | from keys import * 7 | 8 | # NOTE: flush=True is just for running this script 9 | # with PythonAnywhere's always-on task. 10 | # More info: https://help.pythonanywhere.com/pages/AlwaysOnTasks/ 11 | print('this is my twitter bot', flush=True) 12 | 13 | auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) 14 | auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) 15 | api = tweepy.API(auth) 16 | 17 | FILE_NAME = 'last_seen_id.txt' 18 | 19 | def retrieve_last_seen_id(file_name): 20 | f_read = open(file_name, 'r') 21 | last_seen_id = int(f_read.read().strip()) 22 | f_read.close() 23 | return last_seen_id 24 | 25 | def store_last_seen_id(last_seen_id, file_name): 26 | f_write = open(file_name, 'w') 27 | f_write.write(str(last_seen_id)) 28 | f_write.close() 29 | return 30 | 31 | def reply_to_tweets(): 32 | print('retrieving and replying to tweets...', flush=True) 33 | # DEV NOTE: use 1060651988453654528 for testing. 34 | last_seen_id = retrieve_last_seen_id(FILE_NAME) 35 | # NOTE: We need to use tweet_mode='extended' below to show 36 | # all full tweets (with full_text). Without it, long tweets 37 | # would be cut off. 38 | mentions = api.mentions_timeline( 39 | last_seen_id, 40 | tweet_mode='extended') 41 | for mention in reversed(mentions): 42 | print(str(mention.id) + ' - ' + mention.full_text, flush=True) 43 | last_seen_id = mention.id 44 | store_last_seen_id(last_seen_id, FILE_NAME) 45 | if '#helloworld' in mention.full_text.lower(): 46 | print('found #helloworld!', flush=True) 47 | print('responding back...', flush=True) 48 | api.update_status('@' + mention.user.screen_name + 49 | '#HelloWorld back to you!', mention.id) 50 | 51 | while True: 52 | reply_to_tweets() 53 | time.sleep(15) 54 | --------------------------------------------------------------------------------