nvd commited on
Commit
459e8a7
Β·
verified Β·
1 Parent(s): ce243ef

Update bot.py

Browse files
Files changed (1) hide show
  1. bot.py +35 -19
bot.py CHANGED
@@ -4,7 +4,7 @@ import re
4
  import asyncio
5
  import sqlite3
6
  import aiohttp
7
- from bale import Bot, Message, CallbackQuery, Components, InlineKeyboard
8
 
9
  # ---------- Import hardcoded tokens ----------
10
  try:
@@ -58,7 +58,6 @@ def get_chats_to_notify(repo_url: str, new_tag: str):
58
  (repo_url, new_tag)
59
  ).fetchall()
60
  chat_ids = [row[0] for row in rows]
61
- # Update tags for those chats
62
  if chat_ids:
63
  conn.executemany(
64
  "UPDATE subscriptions SET last_tag = ? WHERE chat_id = ? AND repo_url = ?",
@@ -91,17 +90,29 @@ async def fetch_latest_release(owner: str, repo: str, prefer_prerelease: bool =
91
  return releases[0]
92
 
93
  def build_asset_keyboard(release: dict):
 
94
  assets = release.get("assets", [])
95
- comp = Components()
 
 
 
 
 
96
  for i, asset in enumerate(assets):
97
- comp.add_inline_keyboard(
98
- InlineKeyboard(
99
- text=asset["name"][:30], # btn text
100
- callback_data=f"dl:{asset['browser_download_url']}"
101
- ),
102
- row=i // 2
103
  )
104
- return comp
 
 
 
 
 
 
 
 
105
 
106
  # ---------- Bot instance ----------
107
  bot = Bot(token=BALE_TOKEN)
@@ -132,11 +143,14 @@ async def handle_on_demand(message: Message, url: str):
132
  if not release:
133
  await message.reply("Could not fetch releases. The repo may be private or empty.")
134
  return
135
- comp = build_asset_keyboard(release)
136
- if not comp:
137
- await message.reply("No assets found in the latest release.")
 
 
 
138
  else:
139
- await message.reply(f"πŸ“¦ *{release['name']}* – Choose an asset:", components=comp)
140
 
141
  async def handle_watch(message: Message, text: str):
142
  parts = text.split(maxsplit=1)
@@ -148,7 +162,7 @@ async def handle_watch(message: Message, text: str):
148
  if not owner:
149
  await message.reply("Invalid GitHub URL.")
150
  return
151
- # Quick check
152
  rel = await fetch_latest_release(owner, repo)
153
  if not rel:
154
  await message.reply("Could not access releases. The repo might be private or have no releases.")
@@ -162,7 +176,7 @@ async def on_callback(callback: CallbackQuery):
162
  if data.startswith("dl:"):
163
  url = data[3:]
164
  await callback.message.reply("⏳ Downloading file …")
165
- # NO InputFile needed – the library accepts a URL string directly
166
  await bot.send_document(
167
  chat_id=callback.message.chat.id,
168
  document=url,
@@ -183,13 +197,15 @@ async def release_poller():
183
  continue
184
  new_tag = release["tag_name"]
185
  chat_ids = get_chats_to_notify(repo_url, new_tag)
 
 
 
186
  for cid in chat_ids:
187
- comp = build_asset_keyboard(release)
188
  try:
189
  await bot.send_message(
190
  chat_id=int(cid),
191
  text=f"πŸš€ New release for {owner}/{repo}: *{release['name']}*",
192
- components=comp
193
  )
194
  except Exception as e:
195
  print(f"Failed to notify {cid}: {e}")
@@ -200,7 +216,7 @@ async def start_bot(status_callback=None):
200
  init_db()
201
  if status_callback:
202
  status_callback("🟒 Bot is online and polling GitHub for subscriptions.")
203
- # Run both the bot and the poller concurrently
204
  await asyncio.gather(
205
  bot.run(),
206
  release_poller()
 
4
  import asyncio
5
  import sqlite3
6
  import aiohttp
7
+ from bale import Bot, Message, CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton
8
 
9
  # ---------- Import hardcoded tokens ----------
10
  try:
 
58
  (repo_url, new_tag)
59
  ).fetchall()
60
  chat_ids = [row[0] for row in rows]
 
61
  if chat_ids:
62
  conn.executemany(
63
  "UPDATE subscriptions SET last_tag = ? WHERE chat_id = ? AND repo_url = ?",
 
90
  return releases[0]
91
 
92
  def build_asset_keyboard(release: dict):
93
+ """Create an inline keyboard from the release assets."""
94
  assets = release.get("assets", [])
95
+ if not assets:
96
+ return None
97
+
98
+ # Create a list of rows, each row is a list of InlineKeyboardButton
99
+ keyboard_rows = []
100
+ row = []
101
  for i, asset in enumerate(assets):
102
+ # Each button shows the asset name, callback_data holds the download URL
103
+ btn = InlineKeyboardButton(
104
+ text=asset["name"][:30],
105
+ callback_data=f"dl:{asset['browser_download_url']}"
 
 
106
  )
107
+ row.append(btn)
108
+ # 2 buttons per row
109
+ if len(row) == 2:
110
+ keyboard_rows.append(row)
111
+ row = []
112
+ if row: # add any remaining button
113
+ keyboard_rows.append(row)
114
+
115
+ return InlineKeyboardMarkup(keyboard_rows)
116
 
117
  # ---------- Bot instance ----------
118
  bot = Bot(token=BALE_TOKEN)
 
143
  if not release:
144
  await message.reply("Could not fetch releases. The repo may be private or empty.")
145
  return
146
+ markup = build_asset_keyboard(release)
147
+ if markup:
148
+ await message.reply(
149
+ f"πŸ“¦ *{release['name']}* – Choose an asset:",
150
+ reply_markup=markup
151
+ )
152
  else:
153
+ await message.reply("No assets found in the latest release.")
154
 
155
  async def handle_watch(message: Message, text: str):
156
  parts = text.split(maxsplit=1)
 
162
  if not owner:
163
  await message.reply("Invalid GitHub URL.")
164
  return
165
+ # Quick check that the repo exists and has releases
166
  rel = await fetch_latest_release(owner, repo)
167
  if not rel:
168
  await message.reply("Could not access releases. The repo might be private or have no releases.")
 
176
  if data.startswith("dl:"):
177
  url = data[3:]
178
  await callback.message.reply("⏳ Downloading file …")
179
+ # The library supports sending a file via a direct URL
180
  await bot.send_document(
181
  chat_id=callback.message.chat.id,
182
  document=url,
 
197
  continue
198
  new_tag = release["tag_name"]
199
  chat_ids = get_chats_to_notify(repo_url, new_tag)
200
+ if not chat_ids:
201
+ continue
202
+ markup = build_asset_keyboard(release)
203
  for cid in chat_ids:
 
204
  try:
205
  await bot.send_message(
206
  chat_id=int(cid),
207
  text=f"πŸš€ New release for {owner}/{repo}: *{release['name']}*",
208
+ reply_markup=markup
209
  )
210
  except Exception as e:
211
  print(f"Failed to notify {cid}: {e}")
 
216
  init_db()
217
  if status_callback:
218
  status_callback("🟒 Bot is online and polling GitHub for subscriptions.")
219
+ # Run bot and poller concurrently
220
  await asyncio.gather(
221
  bot.run(),
222
  release_poller()