| 6 | if key in os.environ: |
| 7 | return os.environ[key] |
| 8 | else: |
| 9 | print(f"failed to read env {key}") |
| 10 | return "" |
| 11 |
| 12 | APK_FOLDER = "apks" |
| 13 | API_PREFIX = "http://127.0.0.1:38118/bot" + os.environ["BOT_TOKEN"] + "/" |
| 14 |
| 15 | BOT_TARGET = read_env("BOT_TARGET") |
| 16 | ADMIN_USERID = BOT_TARGET.replace("-100","") |
| 17 |
| 18 | VERSION_NAME = read_env("VERSION_NAME") |
| 19 | VERSION_CODE = read_env("VERSION_CODE") |
| 20 | COMMIT_HASH = read_env("GITHUB_SHA") |
| 21 | COMMIT_MESSAGE = read_env("COMMIT_MESSAGE") |
| 22 |
| 23 | APK_CHANNEL_ID = "@NekoXApks" |
| 24 | UPDATE_CHANNEL_ID = "@NekogramX" |
| 25 | UPDATE_METADATA_CHANNEL_ID = "@nekox_update_metadata" |
| 26 | CI_CHANNEL_ID = "@NekoX_CI" |
| 27 |
| 28 |
| 29 | def addEntity(entities, origin_str, en_type, content, url = None) -> str: |
| 30 | origin_len = len(origin_str) |
| 31 | entity = { |
| 32 | "type": en_type, |
| 33 | "offset": origin_len, |
| 34 | "length": len(content) |
| 35 | } |
| 36 | if url: |
| 37 | entity["url"] = url |
| 38 | entities.append(entity) |
| 39 | return content |
| 40 |
| 41 |
| 42 | def waitReply(mid): |
| 43 | last_update = 0 |
| 44 | while True: |
| 45 | print(f"Waiting reply for {mid} offset {last_update}...") |
| 46 | resp = requests.post(API_PREFIX + "getUpdates", json={"allowed_updates":["message"], "timeout": 20, "offset": last_update + 1}) |
| 47 | resp = resp.json() |
| 48 | if not resp["ok"]: |
| 49 | continue |
| 50 | resp = resp["result"] |
| 51 | for update in resp: |
| 52 | if 'message' in update: |
| 53 | msg = update["message"] |
| 54 | if 'from' in msg and str(msg['from']["id"]) == ADMIN_USERID: |
| 55 | if 'reply_to_message' in msg and str(msg["reply_to_message"]["message_id"]) == str(mid): |
| 56 | return msg |
| 57 | for update in resp: |
| 58 | last_update = max(last_update, update["update_id"]) |
| 59 |
| 60 |
| 61 | def sendMessage(message, user_id = BOT_TARGET, entities = None) -> int: |
| 62 | data = { |
| 63 | "chat_id" : user_id, |
| 64 | "text": message, |
| 65 | "entities": entities |
| 66 | } |
| 67 | print(message) |
| 68 | print(entities) |
| 69 | resp = requests.post(API_PREFIX + "sendMessage", json=data).json() |
| 70 | print(resp) |
| 71 | return int(resp["result"]["message_id"]) |
| 72 |
| 73 |
| 74 | def sendDocument(user_id, path, message = "", entities = None): |
| 75 | files = {'document': open(path, 'rb')} |
| 76 | data = {'chat_id': user_id, |
| 77 | 'caption': message, |
| 78 | 'parse_mode': 'Markdown', |
| 79 | 'caption_entities': entities} |
| 80 | response = requests.post(API_PREFIX + "sendDocument", files=files, data=data) |
| 81 | print(response.json()) |
| 82 |
| 83 |
| 84 | def sendRelease(): |
| 85 | apks = os.listdir(APK_FOLDER) |
| 86 | apks.sort() |
| 87 | print(apks) |
| 88 |
| 89 | # read message from admin |
| 90 | mid = sendMessage(f"Please reply the release message for the version {VERSION_NAME},{VERSION_CODE}:", user_id=BOT_TARGET) |
| 91 | admin_resp = waitReply(mid) |
| 92 |
| 93 | # send message and apks to APK channel |
| 94 | message = f"=== {VERSION_NAME} ===" |
| 95 | apk_channel_first_id = sendMessage(message, user_id=APK_CHANNEL_ID) |
| 96 |
| 97 | for apk in apks: |
| 98 | path = os.path.join(APK_FOLDER, apk) |
| 99 | sendDocument(user_id=APK_CHANNEL_ID, path=path) |
| 100 |
| 101 | # generate release message and send to update channel |
| 102 | entities = [] |
| 103 | text = "" |
| 104 | text += addEntity(entities, text, "hashtag", f"#{'PRE_RELEASE' if 'preview' in VERSION_NAME else 'RELEASE'}") |
| 105 | text += " " |
| 106 | text += addEntity(entities, text, "text_link", " ", f'https://t.me/{APK_CHANNEL_ID.replace("@","")}/{apk_channel_first_id + 1}') |
| 107 | text += " " |
| 108 | text += addEntity(entities, text, "bold", VERSION_NAME) |
| 109 | text += "\n\n" |
| 110 | if "entities" not in admin_resp: |
| 111 | admin_resp["entities"] = list() |
| 112 | resp_entities = admin_resp["entities"] |
| 113 | for en in resp_entities: |
| 114 | copy = en.copy() |
| 115 | copy["offset"] += len(text) |
| 116 | entities.append(copy) |
| 117 | text += admin_resp["text"] |
| 118 | text += "\n\n" |
| 119 | text += addEntity(entities, text, "text_link", "GitHub Release", f"https://github.com/NekoX-Dev/NekoX/releases/{VERSION_NAME}") |
| 120 | text += " | " |
| 121 | text += addEntity(entities, text, "text_link", "Apks", f'https://t.me/{APK_CHANNEL_ID.replace("@","")}/{apk_channel_first_id}') |
| 122 | text += " | " |
| 123 | text += addEntity(entities, text, "text_link", "Check Update", "tg://update/") |
| 124 |
| 125 | sendMessage(text, user_id=UPDATE_CHANNEL_ID, entities=entities) |
| 126 |
| 127 | # send release message to metadata channel |
| 128 | mid = sendMessage(admin_resp["text"], user_id=UPDATE_METADATA_CHANNEL_ID, entities=admin_resp["entities"]) |
| 129 | meta_msg = f"{VERSION_NAME},{VERSION_CODE},{apk_channel_first_id},{mid}" |
| 130 | sendMessage(meta_msg, user_id=UPDATE_METADATA_CHANNEL_ID) |
| 131 |
| 132 |
| 133 | def sendCIRelease(): |
| 134 | apks = os.listdir(APK_FOLDER) |
| 135 | apks.sort() |
| 136 | apk = os.path.join(APK_FOLDER, apks[0]) |
| 137 | entities = [] |
| 138 | message = f"CI Build\n\n{COMMIT_MESSAGE}\n\n" |
| 139 | message += addEntity(entities, message, "text_link", COMMIT_HASH[0:8], f"https://github.com/NekoX-Dev/NekoX/commit/{COMMIT_HASH}") |
| 140 | sendDocument(user_id=CI_CHANNEL_ID, path = apk, message=message, ) |
| 141 |
| 142 |
| 143 | if __name__ == '__main__': |
| 144 | print(sys.argv) |
| 145 | if len(sys.argv) != 2: |
| 146 | print("Run Type: release, ci, debug") |
| 147 | sys.stdout.flush() |
| 148 | sys.stderr.flush() |
| 149 | exit(1) |
| 150 | mode = sys.argv[1] |
| 151 | if mode == "release": |
| 152 | sendRelease() |
| 153 | elif mode == "ci": |
| 154 | if COMMIT_MESSAGE.startswith("ci"): |
| 155 | CI_CHANNEL_ID = BOT_TARGET |
| 156 | sendCIRelease() |
| 157 | elif mode == "debug": |
| 158 | APK_CHANNEL_ID = "@test_channel_nekox" |
| 159 | UPDATE_CHANNEL_ID = "@test_channel_nekox" |
| 160 | UPDATE_METADATA_CHANNEL_ID = "@test_channel_nekox" |
| 161 | sendRelease() |
| 162 | else: |
| 163 | print("unknown mode") |
| 164 |
| 165 |