Ejemplo n.º 1
0
    def setup_prerequisite(self):
        DatabaseManager.reset()

        self.bot = Bot(name=u'test', description=u'test',
                       interaction_timeout=120, session_timeout=86400).add()
        DatabaseManager.flush()

        config = {
            'access_token': 'EAAP0okfsZCVkBAI3BCU5s3u8O0iVFh6NAwFHa7X2bKZCGQ'
                            'Lw6VYeTpeTsW5WODeDbekU3ZA0JyVCBSmXq8EqwL1GDuZBO'
                            '7aAlcNEHQ3AZBIx0ZBfFLh95TlJWlLrYetzm9owKNR8Qju8'
                            'HF6qra20ZC6HqNXwGpaP74knlNvQJqUmwZDZD'
        }

        self.platform = Platform(name=u'Test platform',
                                 bot_id=self.bot.id,
                                 type_enum=PlatformTypeEnum.Facebook,
                                 provider_ident='1155924351125985',
                                 config=config).add()
        DatabaseManager.flush()

        self.user_1 = User(platform_id=self.platform.id,
                           platform_user_ident='1153206858057166',
                           last_seen=datetime.datetime(2016, 6, 2, 12, 44, 56,
                                                       tzinfo=pytz.utc),
                           settings={'subscribe': True}).add()
        self.user_2 = User(platform_id=self.platform.id,
                           platform_user_ident='1318395614844436',
                           last_seen=datetime.datetime(2016, 6, 2, 12, 44, 56,
                                                       tzinfo=pytz.utc),
                           settings={'subscribe': True}).add()
        DatabaseManager.commit()
Ejemplo n.º 2
0
def add_user(platform, sender):
    """Add a new user into the system."""
    profile_info = get_user_profile(platform, sender)
    user = User(platform_id=platform.id,
                platform_user_ident=sender,
                last_seen=datetime.datetime.now(),
                **profile_info).add()
    DatabaseManager.commit()

    statsd.gauge('users', User.count(), tags=[config.ENV_TAG])
    track(
        TrackingInfo.Event(sender, '%s.User' % platform.type_enum.value, 'Add',
                           profile_info['first_name']))
    return user
Ejemplo n.º 3
0
    def test_session_mutable_tracking(self):
        bot = reset_and_setup_bots(['test/simple.bot'])[0]
        user = User(platform_id=bot.platforms[0].id,
                    platform_user_ident='',
                    last_seen=datetime.datetime.now(), session=1).add()
        DatabaseManager.commit()

        self.assertNotEquals(User.get_by(id=user.id, single=True), None)

        s = User.get_by(id=user.id, single=True)
        s.session.message_sent = True
        DatabaseManager.commit()

        s = User.get_by(id=user.id, single=True)
        self.assertEquals(s.session.message_sent, True)
Ejemplo n.º 4
0
    def setup_prerequisite(self):
        DatabaseManager.reset()

        self.bot = Bot(name=u'test',
                       description=u'test',
                       interaction_timeout=120,
                       session_timeout=86400).add()
        DatabaseManager.commit()

        config = {
            'access_token':
            'iHRMgmp3zRLOc6kPCbPNMwEDHyFqLGSy0tyG3uZxnkNlhMKg'
            '8GVFqMGslcOkmgOAFLlBvvYuXmKF9odhXtsCm3tBxRcPryKr'
            'kOvzHBcBvS2zrVGiVmZGh5EBcqazgurYMwVSdgNSrhCm/qp6'
            '2aR7HAdB04t89/1O/w1cDnyilFU=',
            'channel_secret':
            '335c901df3a1969ca28a48bf6ddcc333'
        }
        platform = Platform(bot_id=self.bot.id,
                            name=u'Line',
                            type_enum=PlatformTypeEnum.Line,
                            provider_ident='aitjcize.line',
                            config=config).add()
        DatabaseManager.commit()

        self.user = User(
            platform_id=platform.id,
            platform_user_ident='U7200f33369e7e586c973c3a9df8feee4',
            last_seen=datetime.datetime.now()).add()

        DatabaseManager.commit()
Ejemplo n.º 5
0
    def setup_prerequisite(self, bot_file):
        InputTransformation.clear()
        self.bot = reset_and_setup_bots([bot_file])[0]
        self.user = User(platform_id=Platform.get_by(id=1, single=True).id,
                         platform_user_ident='blablabla',
                         last_seen=datetime.datetime.now()).add()

        DatabaseManager.commit()
Ejemplo n.º 6
0
    def Push(self, request, context):
        with DatabaseSession():
            messages_dict = cPickle.loads(request.messages_object)
            users = User.query().filter(User.id.in_(request.user_ids)).all()
            messaging_tasks.push_message_from_dict_async(
                users, messages_dict, request.eta, request.user_localtime)

        return app_service_pb2.Empty()
Ejemplo n.º 7
0
def facebook_webhook_task():
    with statsd.timed('facebook_webhook_task', tags=[config.ENV_TAG]):
        engine = Engine()

        for entry in request.json.get('entry', []):
            page_id = entry['id']
            platform = Platform.get_by(type_enum=PlatformTypeEnum.Facebook,
                                       provider_ident=page_id,
                                       single=True)
            if platform is None:
                raise RuntimeError('no associate bot found for Facebook '
                                   'Platform with page_id = %s' % page_id)

            bot = platform.bot
            g.ga_id = bot.ga_id

            for messaging in entry['messaging']:
                msg = messaging.get('message', None)
                sender = messaging['sender']['id']
                recipient = messaging['recipient']['id']
                user_input = UserInput.FromFacebookMessage(messaging)

                if msg and msg.get('is_echo'):
                    # Ignore message sent by ourself
                    if msg.get('app_id', None):
                        continue
                    user = User.get_by(platform_id=platform.id,
                                       platform_user_ident=recipient,
                                       single=True)
                    engine.process_admin_reply(bot, user, user_input)
                else:
                    user = User.get_by(platform_id=platform.id,
                                       platform_user_ident=sender,
                                       eager=['platform'],
                                       single=True)
                    if not user:
                        user = add_user(platform, sender)

                    if user_input:
                        engine.step(bot, user, user_input)

    # Don't measure the time of the GA call
    send_ga_track_info()
Ejemplo n.º 8
0
    def test_Bot_API(self):
        """Test Bot model APIs."""
        DatabaseManager.reset()

        bots = reset_and_setup_bots(['test/simple.bot', 'test/postback.bot'])
        bot1 = bots[0]
        bot2 = bots[1]

        bot2_node_len = len(bot2.nodes)

        bot1.delete_all_nodes()
        DatabaseManager.commit()

        # All nodes and links related to this bot should be gone.
        self.assertEquals(bot1.nodes, [])

        # Make sure delete_all_nodes does not accidentally delete node
        # of other bot
        self.assertEquals(len(bot2.nodes), bot2_node_len)

        # Test bot reconstruction
        parse_bot_from_file(get_bot_filename('test/simple.bot'), bot1.id)
        DatabaseManager.commit()

        self.assertNotEquals(bot1.nodes, [])
        self.assertEquals(bot1.users, [])

        User(platform_id=bot1.platforms[0].id,
             platform_user_ident='blablabla',
             last_seen=datetime.datetime.now()).add()
        User(platform_id=bot1.platforms[1].id,
             platform_user_ident='blablabla2',
             last_seen=datetime.datetime.now()).add()
        DatabaseManager.commit()
        self.assertEquals(len(bot1.users), 2)

        bot1_id = bot1.id
        bot1.delete()
        DatabaseManager.commit()
        self.assertEquals(Bot.get_by(id=bot1_id, single=True), None)
Ejemplo n.º 9
0
    def Broadcast(self, request, unused_context):
        with DatabaseSession():
            bot = Bot.get_by(id=request.bot_id, single=True)
            if not bot:
                raise RuntimeError('Bot<%d> does not exist' % request.bot_id)

            eta = None if request.eta == 0 else request.eta
            messages_dict = cPickle.loads(request.messages_object)
            if request.static:
                msgs = [Message.FromDict(m, {}) for m in messages_dict]
                messaging_tasks.broadcast_message_async(bot, msgs, eta)
            else:
                users = User.get_by(bot_id=request.bot_id)
                messaging_tasks.push_message_from_dict_async(
                    users, messages_dict, eta)

        return app_service_pb2.Empty()
Ejemplo n.º 10
0
def line_webhook_task(provider_ident):
    with statsd.timed('line_webhook_task', tags=[config.ENV_TAG]):
        engine = Engine()

        for entry in request.json.get('events', []):
            if entry['source']['type'] != 'user':
                raise RuntimeError('line_receive: only user source is support '
                                   'for now')

            sender = entry['source']['userId']
            platform = Platform.get_by(provider_ident=provider_ident,
                                       single=True)
            if platform is None:
                raise RuntimeError('no associate bot found for Line '
                                   'Platform with ident = %s' % provider_ident)
            digest = hmac.new(str(platform.config['channel_secret']),
                              request.data, hashlib.sha256).digest()
            signature = base64.b64encode(digest)

            if signature != request.headers['X-Line-Signature']:
                raise RuntimeError('line_receive: failed to verify message')

            bot = platform.bot
            g.ga_id = bot.ga_id

            user = User.get_by(platform_id=platform.id,
                               platform_user_ident=sender,
                               single=True)
            if not user:
                user = add_user(platform, sender)

            user_input = UserInput.FromLineMessage(entry)
            if user_input:
                g.line_reply_token = entry['replyToken']
                g.line_messages = []
                engine.step(bot, user, user_input)
                line.flush_message(platform)

    # Don't measure the time of the GA call
    send_ga_track_info()
Ejemplo n.º 11
0
    def test_schema_sanity(self):
        """Populate data into all tables and make sure there are no error."""
        DatabaseManager.reset()

        account = Account(name=u'Test Account',
                          email='*****@*****.**', passwd='test_hashed').add()

        bot = Bot(name=u'test', description=u'test', interaction_timeout=120,
                  session_timeout=86400).add()
        account.bots.append(bot)

        content = ContentModule(id='test', name='Content1', description='desc',
                                module_name='', ui_module_name='').add()
        parser = ParserModule(id='test', name='Parser1', description='desc',
                              module_name='passthrough', ui_module_name='',
                              variables={}).add()

        # Test for oauth schema
        oauth1 = OAuthInfo(provider=OAuthProviderEnum.Facebook,
                           provider_ident='mock-facebook-id').add()

        oauth2 = OAuthInfo(provider=OAuthProviderEnum.Github,
                           provider_ident='mock-github-id').add()

        account.oauth_infos.append(oauth1)
        account.oauth_infos.append(oauth2)
        DatabaseManager.commit()

        account_ = Account.get_by(id=account.id, single=True)
        self.assertNotEquals(account_, None)
        self.assertEquals(len(account_.oauth_infos), 2)

        oauth_ = OAuthInfo.get_by(provider_ident='mock-facebook-id',
                                  single=True)
        self.assertNotEquals(oauth_, None)
        self.assertNotEquals(oauth_.account, None)
        self.assertEquals(oauth_.account.id, account.id)

        # Test for bot
        account.bots.append(bot)

        DatabaseManager.commit()

        self.assertNotEquals(Account.get_by(id=account.id, single=True), None)
        self.assertNotEquals(Bot.get_by(id=bot.id, single=True), None)
        self.assertNotEquals(ContentModule.get_by(id=content.id, single=True),
                             None)
        self.assertNotEquals(ParserModule.get_by(id=parser.id, single=True),
                             None)

        # Check acccount_bot association table
        self.assertEquals(len(account.bots), 1)
        self.assertEquals(account.bots[0].id, bot.id)

        platform = Platform(name=u'Test platform',
                            bot_id=bot.id,
                            type_enum=PlatformTypeEnum.Facebook,
                            provider_ident='facebook_page_id',
                            config={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(Platform.get_by(id=platform.id, single=True),
                             None)
        self.assertEquals(len(bot.platforms), 1)
        self.assertEquals(bot.platforms[0].id, platform.id)

        node1 = Node(stable_id='node1', name=u'1', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        node2 = Node(stable_id='node2', name=u'2', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        node3 = Node(stable_id='node3', name=u'3', bot_id=bot.id,
                     expect_input=True, content_module_id=content.id,
                     content_config={}, parser_module_id=parser.id,
                     parser_config={}).add()

        bot.orphan_nodes.append(node3)

        DatabaseManager.commit()

        self.assertNotEquals(Node.get_by(id=node1.id, single=True), None)
        self.assertNotEquals(Node.get_by(id=node2.id, single=True), None)
        self.assertNotEquals(Node.get_by(id=node3.id, single=True), None)

        # Test bot_node association table
        self.assertEquals(bot.orphan_nodes[0].id, node3.id)

        user = User(platform_id=platform.id,
                    platform_user_ident='',
                    last_seen=datetime.datetime.now()).add()
        DatabaseManager.commit()

        self.assertNotEquals(User.get_by(id=user.id, single=True), None)

        event = Event(bot_id=bot.id, user_id=user.id, event_name='event',
                      event_value={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(Event.get_by(id=event.id, single=True), None)

        collected_datum = CollectedDatum(user_id=user.id,
                                         key='key', value={}).add()
        DatabaseManager.commit()

        self.assertNotEquals(CollectedDatum.get_by(id=collected_datum.id,
                                                   single=True), None)
        self.assertEquals(len(user.colleted_data), 1)
        self.assertEquals(user.colleted_data[0].id, collected_datum.id)

        conversation = Conversation(bot_id=bot.id, user_id=user.id,
                                    sender_enum=SenderEnum.Bot, msg={}).add()
        DatabaseManager.commit()
        self.assertNotEquals(Conversation.get_by(id=conversation.id,
                                                 single=True), None)

        # Broadcast
        bc = Broadcast(account_id=account.id, bot_id=bot.id,
                       name=u'New broadcast', messages=[],
                       scheduled_time=datetime.datetime.utcnow()).add()

        DatabaseManager.commit()
        self.assertNotEquals(Broadcast.get_by(id=bc.id, single=True), None)

        # PublicFeed, Feed
        account = Account(name=u'Test Account - 1',
                          email='*****@*****.**', passwd='test_hashed').add()
        feed1 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'foo.com', image_url='foo.com/logo').add()
        feed2 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'bar.com', image_url='bar.com/logo').add()
        feed3 = Feed(url='example.com/rss', type=FeedEnum.RSS,
                     title=u'baz.com', image_url='baz.com/logo').add()

        account.feeds.append(feed1)
        account.feeds.append(feed2)
        account.feeds.append(feed3)

        DatabaseManager.commit()
        self.assertNotEquals(Feed.get_by(id=feed1.id, single=True), None)

        feeds = Feed.search_title('ba')
        self.assertEquals(len(list(feeds)), 2)

        pfeed = PublicFeed(url='example.com/rss', type=FeedEnum.RSS,
                           title=u'example.com',
                           image_url='example.com/logo').add()

        DatabaseManager.commit()
        self.assertNotEquals(PublicFeed.get_by(id=pfeed.id, single=True), None)