def test_send_new_messages_to_slack(monkeypatch, tmp_path, all_messages,
                                    sleepless):
    class MockResponseMessages:
        @staticmethod
        def json():
            return all_messages

    def mock_post(*args, **kwargs):
        return MockResponseMessages()

    def mock_get_permalink(*args, **kwargs):
        return {"permalink": "https://2"}

    def mock_post_message(*args, **kwargs):
        return {"ts": "2"}

    monkeypatch.setattr(requests, "post", mock_post)
    monkeypatch.setattr(WebClient, "chat_getPermalink", mock_get_permalink)
    monkeypatch.setattr(WebClient, "chat_postMessage", mock_post_message)

    mbox_path = tmp_path / "mbox"
    messaging = Messaging("abcd")
    messages = messaging.get_messages_from_game("374955", "1")
    slack_messaging = SlackMessaging("abcd", "#channel")
    slack_messaging.send_new_messages_to_slack(messages, "374955", mbox_path)
    # We don't have a good way to see the messages were posted successfully other
    # than checking they exist in the mailbox afterwards
    mailbox_mbox = mailbox.mbox(mbox_path)
    assert mailbox_mbox.__len__() == 3
示例#2
0
def _mbox(game_id: str, api_key: str, race_id: str, outfile: Path) -> None:
    logger.info("Fetching messages for game %s.", game_id)
    messaging = Messaging(api_key)
    if planets["race_id"]:
        messages = messaging.get_messages_from_game(game_id, race_id)
    else:
        messages = messaging.get_all_messages_from_game(game_id)
    if messages:
        logger.info("Saving messages from game %s to %s.", game_id, outfile)
        exporting = Exporting()
        exporting.to_mbox(messages, outfile)
        logger.info("Messages saved.")
    else:
        logger.error("Could not get messages from game %s", game_id)
        raise typer.Exit()
示例#3
0
def test_get_messages_from_game(monkeypatch, all_messages):
    class MockResponseMessages:
        @staticmethod
        def json():
            return all_messages

    def mock_post(*args, **kwargs):
        return MockResponseMessages()

    monkeypatch.setattr(requests, "post", mock_post)

    messaging = Messaging("abcd")
    messages = messaging.get_messages_from_game("1234", "2")
    assert isinstance(messages, list)
    assert len(messages) == 2
示例#4
0
def csv(outfile: Path = typer.Argument(
    ..., help="Path of the CSV file to use (will be overwritten!)")):
    """Export planets.nu in-game messages to a CSV (comma-separated values) file."""
    logger.info("Fetching messages for game %s.", planets["game_id"])
    messaging = Messaging(planets["api_key"])
    if planets["race_id"]:
        messages = messaging.get_messages_from_game(planets["game_id"],
                                                    planets["race_id"])
    else:
        messages = messaging.get_all_messages_from_game(planets["game_id"])
    if messages:
        logger.info("Saving messages from game %s to %s.", planets["game_id"],
                    outfile)
        exporting = Exporting()
        exporting.to_csv(messages, outfile)
        logger.info("Messages saved.")
    else:
        logger.error("Could not get messages from game %s", planets["game_id"])
示例#5
0
def slack(
        slack_bot_token: str = typer.Option(..., envvar="SLACK_BOT_TOKEN"),
        slack_channel_id: str = typer.Option(..., envvar="SLACK_CHANNEL_ID"),
):
    """Send planets.nu in-game messages to Slack."""
    if not planets["race_id"]:
        logger.error("Player ID required when sending messages to Slack!")
        raise typer.Exit()
    logger.info("Fetching messages for game %s.", planets["game_id"])
    messaging = Messaging(planets["api_key"])
    messages = messaging.get_messages_from_game(planets["game_id"],
                                                planets["race_id"])
    if messages:
        logger.info("Sending messages from game %s to Slack.",
                    planets["game_id"])
        slack_messaging = SlackMessaging(
            slack_bot_token,
            slack_channel_id,
        )
        slack_messaging.send_new_messages_to_slack(messages,
                                                   planets["game_id"])
        logger.info("Messages sent.")
    else:
        logger.error("Could not get messages from game %s", planets["game_id"])