import yaml,json,time,pdb
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from twilio.rest import Client

#retrieve config file with secrets
config = yaml.safe_load(open("config.yml"))
#print(config)
#with open("config.yml",'r') as ymlfile:
#    cfg = yaml.load(ymlfile)
#twitdict= cfg['twitter_set']
twitdict = config['twitter_set']
#twildict= cfg['twilio_set']
twildict = config['twilio_set']
searchdict = config['search_set']
#store twitter secrets in variables
access_token = twitdict['access_token']
access_token_secret = twitdict['access_secret']
consumer_key = twitdict['consumer_key']
consumer_secret = twitdict['consumer_secret']

#store twilio secrets in variables
account = twildict['account']
token = twildict['token']

#get a twilio token


#store call details from config in variables
tophone = twildict['tophone']
smsphone = twildict['sms_notification']
fromphone = twildict['fromphone']
callurl = twildict['url']

#create client object to use for calls or texts
client = Client(account,token)

#store search parameters
ulist=searchdict['user']
tlist=searchdict['track']

#define a basic listener
class CallListener(StreamListener):

    def on_data(self, data):
        print 'matched tweet'
        jsondata=json.loads(data)
        with open('data.txt','w') as outfile:
            json.dump(jsondata,outfile)
        #pdb.set_trace()
        userid = jsondata['user']['id_str']
        if not userid in ulist:
            print 'not one of our users'
            return True
        tweettext=jsondata['text']
        print tweettext.encode('utf-8')
        #pdb.set_trace()
        trigger = 0
        for word in tlist:
            if word in tweettext.lower():
                print 'keyword '+word+' found'
                trigger = 1
        if trigger > 0:
            #print 'make call to '+tophone+' from '+fromphone
            call = client.calls.create(to=tophone, from_=fromphone, url=callurl)
        if trigger == 0:
            print 'no keywords matched; no call made'
        return True

    def on_error(self, code):
        if code == 420:
            message = client.api.account.messages.create(to=smsphone,from_=fromphone,body='420 error, we are down!') 
            return False
        else:
            message = client.api.account.messages.create(to=smsphone,from_=fromphone,body='error code: '+status)
            return False
if __name__ == '__main__':

    #This handles Twitter authetification and the connection to Twitter Streaming API
    l = CallListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)
    #This line filter Twitter Streams to capture tweets from the user ids listed in ulist, which are pulled in from the config file. use the get_ids.py helper script to extract user ids from @handles
    stream.filter(follow=ulist,async=True)

@@ -1134,16 +1134,131 @@
 rack'%5D%0A%0A
+#define message strings%0Aerrtextmess = 'error code: %7B0%7D'%0Aratelimitmess = '420 error! chill out, man! we are down.'%0A%0A
 #define 
@@ -1916,16 +1916,17 @@
 trigger 
++
 = 1%0A    
@@ -2088,32 +2088,34 @@
 allurl)%0A        
+el
 if trigger == 0:
@@ -2338,33 +2338,21 @@
 ody=
-'420 error, we are down!'
+ratelimitmess
 ) %0A 
@@ -2483,29 +2483,29 @@
 ody=
-'error code: '+status
+textmess.format(code)
 )%0A  

change trigger to counter; move text message strings to variables