Refactor Telegram workflow for custom message broadcast#1899
Refactor Telegram workflow for custom message broadcast#1899TheFeloniousMonk wants to merge 1 commit intomainfrom
Conversation
Moved API key to secrets, and added error checking.
There was a problem hiding this comment.
Pull request overview
Refactors the GitHub Actions Telegram workflow to broadcast a manually-entered message, moving the bot token/chat IDs to GitHub Secrets and adding basic API-response error checking.
Changes:
- Switched the Telegram workflow trigger to
workflow_dispatchwith a singlemessageinput. - Replaced hardcoded bot token and chat IDs with
TELEGRAM_BOT_TOKEN/TELEGRAM_CHAT_IDSsecrets (comma-separated list). - Added basic response checking and fails the job if any send fails.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| # Check if the Telegram API returned an error | ||
| if echo "$RESPONSE" | grep -q '"ok":false'; then | ||
| echo "Error sending to $chat_id: $RESPONSE" | ||
| return 1 | ||
| else | ||
| echo "Success: Message sent to $chat_id" |
There was a problem hiding this comment.
The error detection treats any response that does not contain '"ok":false' as success. If curl returns a non-JSON body (proxy error/HTML), an empty response, or a JSON error formatted differently, this can be reported as success. Consider checking the HTTP status/curl exit code and positively validating success (e.g., parse JSON and require ok == true).
| # Check if the Telegram API returned an error | |
| if echo "$RESPONSE" | grep -q '"ok":false'; then | |
| echo "Error sending to $chat_id: $RESPONSE" | |
| return 1 | |
| else | |
| echo "Success: Message sent to $chat_id" | |
| CURL_EXIT_CODE=$? | |
| # First, check if curl itself failed | |
| if [ "$CURL_EXIT_CODE" -ne 0 ]; then | |
| echo "Error sending to $chat_id: curl exited with code $CURL_EXIT_CODE" | |
| return 1 | |
| fi | |
| # Then, positively validate that the Telegram API returned ok == true | |
| if echo "$RESPONSE" | jq -e '.ok == true' > /dev/null 2>&1; then | |
| echo "Success: Message sent to $chat_id" | |
| else | |
| echo "Error sending to $chat_id: $RESPONSE" | |
| return 1 |
| FAILED=0 | ||
| for id in "${ADDR[@]}"; do | ||
| # Trim whitespace | ||
| id=$(echo $id | xargs) |
There was a problem hiding this comment.
id=$(echo $id | xargs) should quote $id to avoid word-splitting/globbing surprises. Using a quoted expansion here makes the trimming robust even if the secret contains unexpected whitespace or characters.
| id=$(echo $id | xargs) | |
| id=$(echo "$id" | xargs) |
|
|
||
| # Check if the Telegram API returned an error | ||
| if echo "$RESPONSE" | grep -q '"ok":false'; then | ||
| echo "Error sending to $chat_id: $RESPONSE" |
There was a problem hiding this comment.
On failure this logs the full Telegram API response ($RESPONSE). Telegram responses can include the message text and chat details, which may be sensitive in GitHub Actions logs. Consider logging only the error description/code (or a redacted subset) instead of the entire response body.
| echo "Error sending to $chat_id: $RESPONSE" | |
| error_code=$(echo "$RESPONSE" | sed -n 's/.*"error_code":\([0-9]*\).*/\1/p') | |
| description=$(echo "$RESPONSE" | sed -n 's/.*"description":"\([^"]*\)".*/\1/p') | |
| echo "Error sending to $chat_id: code=${error_code:-unknown}, description=${description:-unavailable}" |
| @@ -1,109 +1,58 @@ | |||
| name: Telegram Send Message All Groups | |||
| name: Telegram Broadcast Custom Message | |||
There was a problem hiding this comment.
This workflow now has the same name: as .github/workflows/telegram-broadcast.yml (“Telegram Broadcast Custom Message”). In the GitHub Actions UI this makes it hard to distinguish which workflow ran. Consider giving each workflow a distinct name (e.g., one for the dispatcher UI entrypoint vs the reusable sender).
| name: Telegram Broadcast Custom Message | |
| name: Telegram Send Custom Message |
| # This pulls the token and chat IDs from your GitHub Secrets | ||
| TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} | ||
| # Keeping multiple IDs if you have a list, otherwise it will just use the main one | ||
| TELEGRAM_CHAT_IDS: ${{ secrets.TELEGRAM_CHAT_IDS }} | ||
| MESSAGE: ${{ github.event.inputs.message }} |
There was a problem hiding this comment.
There’s no guard for missing/empty TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_IDS. If either secret is unset/empty, the script may attempt requests with an invalid token or an empty chat id, making failures harder to diagnose. Consider adding an explicit early check that both are set and non-empty, and fail with a clear message if not.
| on: | ||
| workflow_call: | ||
| workflow_dispatch: | ||
| inputs: | ||
| message: | ||
| description: "Message to send to all Telegram groups" | ||
| description: 'The message to send to Telegram' | ||
| required: true | ||
| type: string |
There was a problem hiding this comment.
This workflow was converted from a reusable workflow_call to workflow_dispatch and its inputs (parse_mode, test_mode) were removed. However, .github/workflows/telegram-broadcast.yml still calls this file via uses: ./.github/workflows/telegram-send-message.yml and passes parse_mode/test_mode, which requires this workflow to expose on: workflow_call with matching inputs. As-is, that caller workflow will fail to load/run. Consider restoring workflow_call (and inputs), or updating/removing the caller workflow accordingly.
| # This pulls the token and chat IDs from your GitHub Secrets | ||
| TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} | ||
| # Keeping multiple IDs if you have a list, otherwise it will just use the main one | ||
| TELEGRAM_CHAT_IDS: ${{ secrets.TELEGRAM_CHAT_IDS }} | ||
| MESSAGE: ${{ github.event.inputs.message }} | ||
| PARSE_MODE: "Markdown" |
There was a problem hiding this comment.
MESSAGE is sourced from ${{ github.event.inputs.message }}, which works for workflow_dispatch but will not work if this workflow is invoked as a reusable workflow (workflow_call). Using ${{ inputs.message }} is compatible with both patterns and avoids tying the implementation to the event payload shape.
Moved API key to secrets, and added error checking.
Summary
< One line summary>
You can use the following as input for an LLM of your choice to autogenerate a summary (ignoring any additional files needed):
Primary Changes:
Secondary Changes:
Issue
Type of change
Select one or more from the following:
Sanity Checklist
assignees,reviewers,labels,project,iterationandmilestonemake docusaurus_startmake go_develop_and_testandmake test_e2edevnet-test-e2elabel to run E2E tests in CImake test_e2e_oneshot