Automated Direct Message Response using Tweepy
<p>I currently am making use of the tweepy package in python for a DM listener. I wish to send a reply to the sender on reception of their message. I have the following:</p>

<pre><code>class StdOutListener( StreamListener ):
    def __init__( self ):
        self.tweetCount = 0

    def on_connect( self ):
        print("Connection established!!")

    def on_disconnect( self, notice ):
        print("Connection lost!! : ", notice)

    def on_data( self, status ):
        status = str(status)
        try:
            json_acceptable_string = status.replace('\\','')
            #string to dict
            status=json.loads(json_acceptable_string)
            if 'direct_message' in status.keys():
                print '\n'
                print status[u'direct_message'][u'sender_screen_name'] +' sent: '+ status[u'direct_message'][u'text']
                message=str(status[u'direct_message'][u'text'])
                api.send_direct_message(screen_name=str(status[u'direct_message'][u'sender_screen_name']),text='Out of office now - will respond to you asap')
                print 'auto response submitted'
            else:
                #not direct message flow
                pass
        except:
            #not important flows - couldn't convert to json/not correct flow in stream
            pass
        return True

def main():
    global api
    try:
        auth = OAuthHandler(consumer_key, consumer_secret)
        auth.secure = True
        auth.set_access_token(access_token, access_token_secret)
        api = API(auth)
        print(api.me().name)
        stream = Stream(auth, StdOutListener())
        stream.userstream()

    except BaseException as e:
        print("Error in main()", e)

if __name__ == '__main__':
    main()
</code></pre>

<p>For some reason, I can see the print statement of the user and what they sent but when it gets to the send_direct_message method it hangs. 
Oddly enough, if I message myself, I receive a barrage of messages as it loops. Is this because it's on_data()? How can I make this work for other senders?</p>

<p><strong>UPDATE</strong>: Resolved - regnerated tokens and add conditional to check for sender, essentially blacklisting myself.</p>

<p>UPDATE: Resolved - regenerated tokens and add conditional to check for sender, essentially blacklisting myself.</p>

python twitter wrapper tweepy