|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +: "${TELEGRAM_BOT_TOKEN:?TELEGRAM_BOT_TOKEN is required}" |
| 5 | +: "${TELEGRAM_CHAT_ID:?TELEGRAM_CHAT_ID is required}" |
| 6 | + |
| 7 | +APK_DIR="${APK_DIR:-MikuRay/app/build/outputs/apk/${BUILD_TYPE:-debug}}" |
| 8 | +COMMIT_MESSAGE="${TELEGRAM_COMMIT_MESSAGE:-${TELEGRAM_CAPTION:-MikuRay APK build}}" |
| 9 | +COMMIT_SHA="${TELEGRAM_COMMIT_SHA:-}" |
| 10 | +COMMIT_URL="${TELEGRAM_COMMIT_URL:-}" |
| 11 | +MAX_FILE_BYTES="${MAX_FILE_BYTES:-50000000}" |
| 12 | + |
| 13 | +escape_html() { |
| 14 | + local value="$1" |
| 15 | + value="${value//&/&}" |
| 16 | + value="${value//</\<}" |
| 17 | + value="${value//>/\>}" |
| 18 | + value="${value//\"/\"}" |
| 19 | + printf '%s' "${value}" |
| 20 | +} |
| 21 | + |
| 22 | +escaped_commit_message="$(escape_html "${COMMIT_MESSAGE}")" |
| 23 | +if [[ -n "${COMMIT_SHA}" ]]; then |
| 24 | + commit_label="${escaped_commit_message} (${COMMIT_SHA})" |
| 25 | + plain_commit_label="${COMMIT_MESSAGE} (${COMMIT_SHA})" |
| 26 | +else |
| 27 | + commit_label="${escaped_commit_message}" |
| 28 | + plain_commit_label="${COMMIT_MESSAGE}" |
| 29 | +fi |
| 30 | +if [[ -n "${COMMIT_URL}" ]]; then |
| 31 | + CAPTION="<a href=\"${COMMIT_URL}\">${commit_label}</a>" |
| 32 | + PLAIN_CAPTION="${plain_commit_label}"$'\n'"${COMMIT_URL}" |
| 33 | +else |
| 34 | + CAPTION="${commit_label}" |
| 35 | + PLAIN_CAPTION="${plain_commit_label}" |
| 36 | +fi |
| 37 | + |
| 38 | +shopt -s nullglob |
| 39 | +apk_files=( |
| 40 | + "${APK_DIR}"/*arm64-v8a*.apk |
| 41 | + "${APK_DIR}"/*armeabi-v7a*.apk |
| 42 | +) |
| 43 | + |
| 44 | +if (( ${#apk_files[@]} == 0 )); then |
| 45 | + echo "No ARM APK files found in ${APK_DIR}." |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +LAST_RESPONSE="" |
| 50 | +LAST_CURL_EXIT=0 |
| 51 | + |
| 52 | +send_document() { |
| 53 | + local apk_file="$1" |
| 54 | + local caption="$2" |
| 55 | + local use_html="$3" |
| 56 | + local response |
| 57 | + local curl_exit |
| 58 | + local -a curl_args |
| 59 | + |
| 60 | + curl_args=( |
| 61 | + --fail-with-body |
| 62 | + --silent |
| 63 | + --show-error |
| 64 | + --retry 3 |
| 65 | + --retry-delay 5 |
| 66 | + --request POST |
| 67 | + "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendDocument" |
| 68 | + --form "chat_id=${TELEGRAM_CHAT_ID}" |
| 69 | + --form "document=@${apk_file}" |
| 70 | + --form-string "caption=${caption}" |
| 71 | + --form-string "disable_notification=false" |
| 72 | + ) |
| 73 | + if [[ "${use_html}" == "html" ]]; then |
| 74 | + curl_args+=(--form-string "parse_mode=HTML") |
| 75 | + fi |
| 76 | + |
| 77 | + if response="$(curl "${curl_args[@]}" 2>&1)"; then |
| 78 | + curl_exit=0 |
| 79 | + else |
| 80 | + curl_exit=$? |
| 81 | + fi |
| 82 | + LAST_RESPONSE="${response}" |
| 83 | + LAST_CURL_EXIT="${curl_exit}" |
| 84 | + |
| 85 | + if [[ "${curl_exit}" -eq 0 ]] && grep -Eq '"ok"[[:space:]]*:[[:space:]]*true' <<<"${response}"; then |
| 86 | + return 0 |
| 87 | + fi |
| 88 | + return 1 |
| 89 | +} |
| 90 | + |
| 91 | +is_html_error() { |
| 92 | + grep -Eiq "can't parse entities|unsupported start tag|unclosed tag" <<<"${LAST_RESPONSE}" |
| 93 | +} |
| 94 | + |
| 95 | +failure_exit_code() { |
| 96 | + if [[ "${LAST_CURL_EXIT}" -ne 0 ]]; then |
| 97 | + printf '%s' "${LAST_CURL_EXIT}" |
| 98 | + else |
| 99 | + printf '1' |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +for apk_file in "${apk_files[@]}"; do |
| 104 | + file_name="$(basename "${apk_file}")" |
| 105 | + file_size="$(stat -c '%s' "${apk_file}")" |
| 106 | + if (( file_size > MAX_FILE_BYTES )); then |
| 107 | + echo "Telegram upload skipped for ${file_name}: file size ${file_size} bytes exceeds ${MAX_FILE_BYTES}-byte limit." >&2 |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + |
| 111 | + echo "Uploading ${file_name} to Telegram as an individual document..." |
| 112 | + if ! send_document "${apk_file}" "${CAPTION}" html; then |
| 113 | + if is_html_error; then |
| 114 | + echo "HTML caption was rejected; retrying ${file_name} with a plain-text caption." >&2 |
| 115 | + if ! send_document "${apk_file}" "${PLAIN_CAPTION}" plain; then |
| 116 | + echo "Telegram upload failed for ${file_name} (curl exit ${LAST_CURL_EXIT})." >&2 |
| 117 | + echo "Telegram response: ${LAST_RESPONSE}" >&2 |
| 118 | + exit "$(failure_exit_code)" |
| 119 | + fi |
| 120 | + else |
| 121 | + echo "Telegram upload failed for ${file_name} (curl exit ${LAST_CURL_EXIT})." >&2 |
| 122 | + echo "Telegram response: ${LAST_RESPONSE}" >&2 |
| 123 | + exit "$(failure_exit_code)" |
| 124 | + fi |
| 125 | + fi |
| 126 | +done |
| 127 | + |
| 128 | +echo "Uploaded ${#apk_files[@]} ARM APK file(s) to Telegram in separate messages." |
0 commit comments