I've written this script to send an SMS to myself when the file results.txt is modified. It sends the contents of the file in a text to my phone (i've changed the numbers in the question). It works, apart from the fact that I receive two identical SMS messages per modification and I can't for the life of me work out why.
import time
import sys, os
from twilio.rest import Client
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
class MyHandler(PatternMatchingEventHandler):
patterns = ["./results.txt"]
def process(self, event):
with open('results.txt', 'r') as myfile:
newtweet=myfile.read().replace('\n', '')
client = Client("ACac14lkjdchlkdhfdhb448d175335fbd8", "hduhdhdhddhidh39837382783232")
client.messages.create(from_="+44145698039",
to="+44778974188",
body=newtweet)
def on_modified(self, event):
self.process(event)
if __name__ == '__main__':
args = sys.argv[1:]
observer = Observer()
observer.schedule(MyHandler(), path=args[0] if args else '.')
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
EDIT**
here is the file that's writing to results.txt:
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
text = status.text
name = status.user.screen_name
uid = status.user.id
print (text)
print (uid, text, name, file=open("results.txt", "w"))
def on_error(self, status_code):
if status_code == 420:
return False
myStreamListener = MyStreamListener()
users = ['22776208', '2845678578']
stream = tweepy.Stream(auth = api.auth, listener=myStreamListener, include_entities=True)
stream.filter(follow=users, languages=["en"])
keywords = ["Trump"]
stream.filter(track=keywords, languages=["en"])