Ejemplo n.º 1
0
 def test_analyze_relative_day_day_before_tomorrow(self):
     result = KeywordAnalyzer("kult vorgestern").analyze()
     expected_date = (datetime.date.today() +
                      datetime.timedelta(days=-2)).strftime(
                          DateFormats.COMMON)
     self.assertEqual(True, result.is_triggered())
     self.assertEqual(True, result.is_relative_day())
     self.assertEqual(expected_date, result.get_date())
     self.assertEqual("kult", result.triggered_word)
Ejemplo n.º 2
0
def slack_event_handler(event_type, slack_event):
    """
    A helper function that routes events from Slack to our Bot
    by event type and subtype.

    Parameters
    ----------
    event_type : str
        type of event received from Slack
    slack_event : dict
        JSON response from a Slack reaction event

    Returns
    ----------
    obj
        Response object with 200 - ok or 501 - Not implemented

    """
    team_id = slack_event["team_id"]

    if event_type == "message":
        event = slack_event["event"]
        subtype = event.get("subtype")
        timestamp = event.get("ts")
        message = event.get("text")
        channel = event.get("channel")
        is_my_bot = subtype == 'bot_message' and pyBot.get_name() in event.get(
            "username")

        key = team_id + '_' + message + '_' + timestamp

        duplicate_message = key in list(pyBot.get_messages())
        if duplicate_message or is_my_bot:
            logging.debug("Already answered message: " + message)
            return make_response("Already answered", 200)
        analyzer = KeywordAnalyzer(message).analyze()
        pyBot.append_message(key)
        if analyzer.is_triggered():
            logging.info("Triggered bot")
            menu_holder = kult_menuholder

            if analyzer.is_today():
                menu_text = menu_holder.get_menu_text_by_date(
                    datetime.date.today().strftime(DateFormats.COMMON))
            elif analyzer.is_relative_day():
                menu_text = menu_holder.get_menu_text_by_date(
                    analyzer.get_date())
            elif analyzer.is_special():
                menu_text = random.choice([
                    "(╯°□°)╯︵ ┻━┻", "(┛◉Д◉)┛彡┻━┻", "(ノಥ,_」ಥ)ノ彡┻━┻",
                    "(ノ ̄皿 ̄)ノ ⌒=== ┫"
                ])
            elif analyzer.is_weekly():
                menu_text = str(menu_holder.get_current_menu())
            else:
                menu_text = menu_holder.get_menu_text_by_weekday(
                    analyzer.get_day())

            logging.info("Send menu: " + menu_text)
            pyBot.send_message(menu_text, channel, team_id)
            return make_response("Posted food into channel", 200)
    elif event_type == "app_uninstalled":
        logging.info("bot was uninstalled - remove oauth token from database")
        auth_token.remove(team_id)
        return make_response("Removed app", 200)

    # Show not implemented if bot doesnt know how to handle this event
    message = "You have not added an event handler for the %s" % event_type
    logging.warning(message)
    return make_response(message, 501, {"X-Slack-No-Retry": 1})
Ejemplo n.º 3
0
 def test_is_weekly(self):
     result = KeywordAnalyzer('woche').analyze()
     self.assertEqual(True, result.is_triggered())
     self.assertEqual(True, result.is_weekly())
     self.assertEqual(False, result.is_relative_day())
     self.assertEqual(False, result.is_today())
Ejemplo n.º 4
0
 def test_is_special(self):
     result = KeywordAnalyzer(":zornig:").analyze()
     self.assertEqual(True, result.is_triggered())
     self.assertEqual(True, result.is_special())
     self.assertEqual(False, result.is_relative_day())
     self.assertEqual(False, result.is_today())
Ejemplo n.º 5
0
 def test_analyze_monday_koelle(self):
     result = KeywordAnalyzer("kölle montag").analyze()
     self.assertEqual(True, result.is_triggered())
     self.assertEqual(False, result.is_relative_day())
     self.assertEqual('Montag', result.get_day())
     self.assertEqual("kölle", result.triggered_word)