Skip to content

Instantly share code, notes, and snippets.

@Aniket-508
Created January 13, 2022 16:53
Show Gist options
  • Save Aniket-508/85c90e5a70e6f85fd8da6b4a3ea552e8 to your computer and use it in GitHub Desktop.
Save Aniket-508/85c90e5a70e6f85fd8da6b4a3ea552e8 to your computer and use it in GitHub Desktop.
Twitter bot to directly reply to mentions.
import tweepy
import logging
from config import create_api
import time
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
def check_mentions(api, keywords, since_id):
logger.info("Retrieving mentions")
new_since_id = since_id
for tweet in tweepy.Cursor(api.mentions_timeline,
since_id=since_id).items():
new_since_id = max(tweet.id, new_since_id)
if tweet.in_reply_to_status_id is not None:
continue
if any(keyword in tweet.text.lower() for keyword in keywords):
logger.info(f"Answering to {tweet.user.name}")
if not tweet.user.following:
tweet.user.follow()
api.update_status(
status="Please reach us via DM",
in_reply_to_status_id=tweet.id,
)
return new_since_id
def main():
api = create_api()
since_id = 1
while True:
since_id = check_mentions(api, ["help", "support"], since_id)
logger.info("Waiting...")
time.sleep(60)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment