Beispiel #1
0
def handle_new_message(data):
    db.session.add(
        models.Messages(
            data["username"],
            data["messageText"][0:256],
            datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            models.Users.query.filter_by(username=data["username"]).first().imageUrl,
        )
    )
    if data["messageText"][0:2] == "!!":
        db.session.add(
            models.Messages(
                "butler-bot",
                chatbot.respond(data["messageText"]),
                datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                "https://i.imgur.com/m9mlpmh.png",
            )
        )
    db.session.commit()
    update_messages()
    return models.Messages(
        data["username"],
        data["messageText"][0:256],
        datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        models.Users.query.filter_by(username=data["username"]).first().imageUrl,
    )
Beispiel #2
0
    def on_data(self, data):
        print data
        tweet = json.loads(data.strip())
        
        retweeted = tweet.get('retweeted')
        from_self = tweet.get('user',{}).get('id_str','') == account_user_id

        if retweeted is not None and not retweeted and not from_self:

            tweetId = tweet.get('id_str')
            screenName = tweet.get('user',{}).get('screen_name')
            tweetText = tweet.get('text')

            chatResponse = chatbot.respond(tweetText)

            replyText = '@' + screenName + ' ' + chatResponse

            #check if repsonse is over 140 char
            if len(replyText) > 140:
                replyText = replyText[0:139] + '…'

            print('Tweet ID: ' + tweetId)
            print('From: ' + screenName)
            print('Tweet Text: ' + tweetText)
            print('Reply Text: ' + replyText)

            # If rate limited, the status posts should be queued up and sent on an interval
            twitterApi.update_status(status=replyText, in_reply_to_status_id=tweetId)
    def test_success(self):
        for test_case in self.success_test_params:
            if (test_case[KEY_ID] == 1):
                with mock.patch('requests.get', self.mocked_funtranslate):
                    inp = chatbot.respond(test_case[KEY_INPUT])

                expected = test_case[KEY_EXPECTED]

                self.assertEqual(inp, expected[KEY_RESPONSE])

            elif (test_case[KEY_ID] == 2):
                try:
                    with mock.patch('app.db.session.add',
                                    self.mocked_db_add_user):
                        with mock.patch('app.update_user_count', self.empty):
                            with mock.patch('app.update_messages', self.empty):
                                with mock.patch('app.update_user_count',
                                                self.empty):
                                    test = app.on_username_request(
                                        test_case[KEY_INPUT])
                                    result = "No TypeErrors with Input"
                except ValueError:
                    result = "ValueError"

                expected = test_case[KEY_EXPECTED]
                self.assertEqual(result, expected[KEY_RESPONSE])

            elif (test_case[KEY_ID] == 3):
                try:
                    with mock.patch('sqlalchemy.orm.query.Query.filter',
                                    self.mocked_get_userimage):
                        with mock.patch('app.db.session.add',
                                        self.mocked_db_add_message):
                            with mock.patch('app.update_messages', self.empty):
                                result = app.handle_new_message(
                                    test_case[KEY_INPUT]).imageUrl
                except AttributeError:
                    result = "Attribute Error for Image"

                expected = test_case[KEY_EXPECTED]
                self.assertEqual(result, expected[KEY_RESPONSE])

            elif (test_case[KEY_ID] == 4):
                try:
                    with mock.patch('sqlalchemy.orm.query.Query.filter',
                                    self.mocked_get_userimage):
                        with mock.patch('app.db.session.add',
                                        self.mocked_db_add_message):
                            with mock.patch('app.update_messages', self.empty):
                                result = app.handle_new_message(
                                    test_case[KEY_INPUT]).messageText
                except ButlerCommandException:
                    result = "Butler Command Detected"

                expected = test_case[KEY_EXPECTED]
                self.assertEqual(result, expected[KEY_RESPONSE])
Beispiel #4
0
        async def on_message(message):

            if message.author.bot or str(message.channel) != self.channel:
                return

            text = message.content
            for ch in [
                    '/', "'", ".", "\\", "(", ")", '"', '\n', '@', '<', '>'
            ]:
                text = text.replace(ch, '')
            response = chatbot.respond(text)
            await message.channel.send(response)
Beispiel #5
0
    def model_api(input_data):
        """
            Args:
            input_data: submitted to the API, raw string
             Returns:
            output_data: after some transformation, to be
                returned to the API
        """

        # 4. process the output
        output_data = chatbot.respond(input_data)
        # 5. return the output for the api
        return output_data
Beispiel #6
0
def handle_command(command, channel):
    """
        Receives commands directed at the bot and determines if they
        are valid commands. If so, then acts on the commands. If not,
        returns back what it needs for clarification.
    """

    response = respond(command)
    print(response)
    response = text_to_gif(response)
    slack_client.api_call("chat.postMessage",
                          channel=channel,
                          text=response,
                          as_user=True)
def chat():
    request_data = request.get_json()
    message = request_data['message']
    state = request_data['state']
    params = request_data['params']
    suggestion = request_data['suggestion']
    actions = request_data['actions']
    new_state, response, params, suggestion, actions = respond(
        state, message, params, suggestion, actions)
    result = {
        'state': new_state,
        'message': response,
        'params': params,
        'suggestion': suggestion,
        'actions': actions
    }
    return jsonify(result)
    def test_chatbot_message_success(self):
        for test in self.success_test_params:
            inp = chatbot.respond(test[KEY_INPUT])
            expected = test[KEY_EXPECTED]

            self.assertEqual(inp, expected[KEY_RESPONSE])