Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Slack payload template #73

Merged
merged 2 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions arxiv_post/apps/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,44 @@


# constants
PAYLOAD_TOML = """
text = "{header}"
PAYLOAD_TOML = '''
text = """{header}"""

[[blocks]]
type = "header"

[blocks.text]
type = "plain_text"
text = "{header}"
text = """{header}"""

[[blocks]]
type = "section"

[blocks.text]
type = "mrkdwn"
text = "*Titie:* {title}"
text = """*Titie:* {title}"""

[[blocks]]
type = "section"

[blocks.text]
type = "mrkdwn"
text = "*Authors:* {authors}"
text = """*Authors:* {authors}"""

[[blocks]]
type = "section"

[blocks.text]
type = "mrkdwn"
text = "*Summary:* {summary}"
text = """*Summary:* {summary}"""

[[blocks]]
type = "actions"

[[blocks.elements]]
type = "button"
action_id = "view_arxiv"
url = "{arxiv_url}"
url = """{arxiv_url}"""

[blocks.elements.text]
type = "plain_text"
Expand All @@ -62,27 +62,31 @@
[[blocks.elements]]
type = "button"
action_id = "view_pdf"
url = "{arxiv_pdf_url}"
url = """{arxiv_pdf_url}"""

[blocks.elements.text]
type = "plain_text"
text = "View PDF"
"""
'''


# logger
logger = getLogger(__name__)


# runtime functions
def post(articles: Sequence[Article], webhook_url: str) -> None:
def post(articles: Sequence[Article], webhook_url: str, dry_run: bool) -> None:
"""Post articles to Slack."""
for article in articles:
try:
_post(webhook_url, json=to_payload(article))
logger.debug(f"Posted {article.arxiv_url}")
payload = to_payload(article)

if not dry_run:
_post(webhook_url, json=payload)

logger.debug(f"Posted an article ({article.arxiv_url})")
except TOMLDecodeError:
logger.warn(f"Failed to post {article.arxiv_url}")
logger.warn(f"Failed to post an article ({article.arxiv_url})")


def to_payload(article: Article) -> Dict[str, Any]:
Expand Down
4 changes: 3 additions & 1 deletion arxiv_post/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def cmd_slack(
n_concurrent: int = N_CONCURRENT,
timeout: float = TIMEOUT,
webhook_url: str = "",
dry_run: bool = False,
debug: bool = False,
) -> None:
"""Translate and post articles to Slack.
Expand All @@ -63,6 +64,7 @@ def cmd_slack(
n_concurrent: Number of simultaneous execution.
timeout: Timeout for each post execution (in seconds).
webhook_url: URL of Slack incoming webhook.
dry_run: If True, articles are not posted to Slack.
debug: If True, debug-level log messages are shown.

Returns:
Expand Down Expand Up @@ -94,7 +96,7 @@ def cmd_slack(
timeout=timeout,
)

return slack.post(translated, webhook_url)
return slack.post(translated, webhook_url, dry_run)


def cli() -> None:
Expand Down