Skip to content

Commit

Permalink
Merge pull request #73 from astropenguin/#72-fix-payload
Browse files Browse the repository at this point in the history
Fix Slack payload template
  • Loading branch information
astropenguin committed Oct 31, 2021
2 parents 4dffcd2 + d99421b commit 5789bec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
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

0 comments on commit 5789bec

Please sign in to comment.