Example #1
0
def test_dont_send_message_if_already_following(mocker, twit_mock, create_bot):
    ''' Twitter bot should check the follower at start and follow the new ones.
    '''

    mAPI = mocker.patch('tweepy.API')
    twit_mock.set_API(mAPI)

    class MyBot(Bot):
        'Echo bot'

        @command
        def start(self):
            return 'Hello new friend!'

    tep = TwitterEndpoint(consumer_key='',
                          consumer_secret='',
                          access_token='',
                          access_token_secret='')
    twit_mock.set_endpoint(tep)
    user = twit_mock.add_follower('new_friend')
    twit_mock.create_friendship(user['id'])

    create_bot(MyBot(), tep)

    mAPI().create_friendship.assert_not_called()
    mAPI().send_direct_message.assert_not_called()
Example #2
0
def test_use_start_on_twitter_follow(mocker, twit_mock, create_bot):
    ''' Twitter bot should auto-follow the followers and then use the start
        command.
    '''

    mAPI = mocker.patch('tweepy.API')
    twit_mock.set_API(mAPI)

    class MyBot(Bot):
        'Echo bot'

        @command
        def start(self):
            return 'Hello new friend!'

    tep = TwitterEndpoint(consumer_key='',
                          consumer_secret='',
                          access_token='',
                          access_token_secret='')
    twit_mock.set_endpoint(tep)
    create_bot(MyBot(), tep)

    user = twit_mock.add_follower('new_friend')

    mAPI().create_friendship.assert_called_once_with(user_id=user['id'])
    mAPI().send_direct_message.assert_called_once_with(
        text='Hello new friend!', user_id=user['id'])
Example #3
0
def test_dont_process_old_dms(mocker, twit_mock, create_bot):
    ''' Test that the Twitter bot ignore the DMs sent before its start.
    '''

    mAPI = mocker.patch('tweepy.API')

    class MyBot(Bot):
        'Echo bot'

        def default_response(self, in_message):
            return in_message

    tep = TwitterEndpoint(consumer_key='',
                          consumer_secret='',
                          access_token='',
                          access_token_secret='')
    twit_mock.set_endpoint(tep)

    twit_mock.add_direct_message('previous message')

    create_bot(MyBot(), tep)

    message = twit_mock.add_direct_message('this is the first message')
    mAPI().send_direct_message.assert_called_once_with(
        text='this is the first message', user_id=message['sender']['id'])

    message = twit_mock.add_direct_message('this is the last message')
    assert mAPI().send_direct_message.call_count == 2
    mAPI().send_direct_message.assert_called_with(
        text='this is the last message', user_id=message['sender']['id'])
Example #4
0
def test_twitter_default_response(mocker, twit_mock, create_bot):
    ''' Test that the Twitter bot correctly reply with the default response.
    '''

    mAPI = mocker.patch('tweepy.API')
    mocker.patch('tweepy.StreamListener')

    class MyBot(Bot):
        'Upp&Down bot'

        def default_response(self, in_message):
            return ''.join([
                c.upper() if i % 2 else c.lower()
                for i, c in enumerate(in_message)
            ])

    tep = TwitterEndpoint(consumer_key='',
                          consumer_secret='',
                          access_token='',
                          access_token_secret='')
    twit_mock.set_endpoint(tep)
    create_bot(MyBot(), tep)

    message = twit_mock.add_direct_message('SuperCamelCase')
    mAPI().send_direct_message.assert_called_with(
        text='sUpErCaMeLcAsE', user_id=message['sender']['id'])

    message = twit_mock.add_direct_message('plain boring text')
    mAPI().send_direct_message.assert_called_with(
        text='pLaIn bOrInG TeXt', user_id=message['sender']['id'])
Example #5
0
def test_twitter_interface(mocker, create_bot):
    ''' Test that the Twitter API is called when using the endpoint.
    '''

    mOAuthHandler = mocker.patch('tweepy.OAuthHandler')
    mAPI = mocker.patch('tweepy.API')

    class MyBot(Bot):
        'Lowering bot'

        def default_response(self, in_message):
            return in_message.lower()

    consumer_key = 'consumer_key',
    consumer_secret = 'consumer_secret',
    access_token = 'access_token',
    access_token_secret = 'access_token_secret'

    tep = TwitterEndpoint(consumer_key=consumer_key,
                          consumer_secret=consumer_secret,
                          access_token=access_token,
                          access_token_secret=access_token_secret)

    bot = create_bot(MyBot(), tep)

    assert bot.endpoints[0]._bot == bot

    mOAuthHandler.assert_called_once_with(consumer_key, consumer_secret)
    mOAuthHandler().set_access_token.assert_called_once_with(
        access_token, access_token_secret)
    mAPI.assert_called_once_with(mOAuthHandler())
Example #6
0
def test_twitter_commands(mocker, twit_mock, create_bot):
    ''' Test that the Twitter bot handles commands.
    '''

    mAPI = mocker.patch('tweepy.API')

    class MyBot(Bot):
        'Echo bot'

        @command
        def start(self):
            return 'Hello!'

    tep = TwitterEndpoint(consumer_key='',
                          consumer_secret='',
                          access_token='',
                          access_token_secret='')
    twit_mock.set_endpoint(tep)
    create_bot(MyBot(), tep)

    message = twit_mock.add_direct_message('/start')
    mAPI().send_direct_message.assert_called_once_with(
        text='Hello!', user_id=message['sender']['id'])
Example #7
0
#!/usr/bin/env python3

import os
import logging

from eddie.endpoints import HttpEndpoint, TelegramEndpoint, TwitterEndpoint
from example_bot import ExampleBot

log_level = os.environ.get('BOT_LOGLEVEL', 'ERROR')
logging.basicConfig(level=log_level)

if __name__ == "__main__":
    bot = ExampleBot()

    bot.add_endpoint(HttpEndpoint(port=int(os.environ['PORT'])))

    bot.add_endpoint(TelegramEndpoint(token=os.environ['BOT_TG_TOKEN']))

    bot.add_endpoint(
        TwitterEndpoint(
            consumer_key=os.environ['BOT_TW_consumer_key'],
            consumer_secret=os.environ['BOT_TW_consumer_secret'],
            access_token=os.environ['BOT_TW_access_token'],
            access_token_secret=os.environ['BOT_TW_access_token_secret']))

    bot.run()

    logging.info("Serving...")