Removed_Content

Reddit-SMS-Notifier.py

Feb 20th, 2026 (edited)
147
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | Source Code | 0 0
  1. """
  2. Reddit-SMS-Notifier.py
  3.  
  4. Monitors a subreddit for new posts matching a search term, and sends an SMS
  5. alert via Twilio whenever a matching post is found. Keeps a local record of
  6. already-seen post IDs (idArray.p) to avoid duplicate notifications, and logs
  7. activity to a timestamped file in ./logs/.
  8.  
  9. Requires: praw, twilio
  10. Fill in your Reddit API credentials, Twilio credentials, and search settings
  11. in the __main__ block before running.
  12. """
  13.  
  14. import datetime
  15. import os, sys
  16. import pickle
  17. import praw
  18. import time
  19. from twilio.rest import Client
  20.  
  21. def createLog(message):
  22.     logFile.write(f"[{getCurrentTime()}] {message}")
  23.     logFile.flush()
  24.  
  25. def getCurrentTime():
  26.     now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  27.     return now
  28.  
  29. def loadArray():
  30.     if os.path.isfile("./idArray.p"):
  31.         fileIn = open("./idArray.p", "rb")
  32.         idArray = pickle.load(fileIn)
  33.         fileIn.close()
  34.     else:
  35.         idArray = []
  36.     return idArray
  37.  
  38. def saveArray():
  39.     fileOut = open("./idArray.p", "wb")
  40.     pickle.dump(idArray, fileOut)
  41.     fileOut.close()
  42.  
  43. def getNewSubmissions():
  44.     createLog(f"Getting {postLimit} new posts\n")
  45.  
  46.     for submission in reddit.subreddit(subredditName).new(limit=postLimit):
  47.         if searchQuery in submission.title.lower():
  48.             if submission.id not in idArray:
  49.                 idArray.append(submission.id)
  50.                 saveArray()
  51.  
  52.                 createLog(f"Found new post: {submission.title}\n")
  53.  
  54.                 try:
  55.                     message = client.messages.create(
  56.                         body=f"New Update: {submission.title} \n\nVisit the link here: {submission.url}",
  57.                         from_=sendingPhoneNumber,
  58.                         to=destinationPhoneNumber,
  59.                     )
  60.                     time.sleep(5)
  61.                     createLog(f"Text sent successfully\n")
  62.                     createLog(f"Message ID: {message.sid}\n")
  63.                 except Exception as e:
  64.                     createLog(f"Text did not send successfully: {e}\n")
  65.         else:
  66.             createLog(f"Submission doesn't contain correct criteria\n")
  67.  
  68.     createLog(f"Sleeping for {1 * timeDelay} minutes\n\n")
  69.     try:
  70.         time.sleep(60 * timeDelay)
  71.     except KeyboardInterrupt:
  72.         createLog(f"Shutdown received\n")
  73.         logFile.close()
  74.         sys.exit()
  75.  
  76. if __name__ == "__main__":
  77.     accountSid = ""
  78.     authToken = ""
  79.     client = Client(accountSid, authToken)
  80.     destinationPhoneNumber = ""
  81.     logFile = open(f"./logs/log {getCurrentTime().replace(':', '')}.txt", "a+")
  82.     postLimit = 15
  83.     reddit = praw.Reddit(
  84.         client_id="",
  85.         client_secret="",
  86.         user_agent=""
  87.     )
  88.     searchQuery = ""
  89.     sendingPhoneNumber = ""
  90.     subredditName = ""
  91.     timeDelay = 5
  92.  
  93.     idArray = loadArray()
  94.  
  95.     while True:
  96.         getNewSubmissions()
  97.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment