Skip to content

Commit 0dd1b9b

Browse files
committed
Add a setting to control truncation limit (max msg length)
1 parent 3e1b166 commit 0dd1b9b

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

bot/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
GITHUB_OAUTH_CLIENT_SECRET = os.getenv('GITHUB_OAUTH_CLIENT_SECRET')
1515
GITHUB_OAUTH_REDIRECT_URI = SERVER_URL_BASE + '/github/auth'
1616
DEBUG = os.getenv('DEBUG', False)
17+
DEFAULT_TRUNCATION_LIMIT = 4096

bot/github.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from telegram import ParseMode
55
from telegram.ext import CallbackContext, Dispatcher
66

7+
from bot.const import DEFAULT_TRUNCATION_LIMIT
78
from bot.githubapi import github_api
89
from bot.githubupdates import GithubAuthUpdate, GithubUpdate
910
from bot.menu import edit_menu_by_id
@@ -55,13 +56,19 @@ def _iter_repos(self, repository):
5556
if 'repos' in chat_data:
5657
for repo in chat_data['repos'].values():
5758
if repo.id == repo_id:
58-
yield chat_id, repo
59+
yield chat_id, chat_data, repo
5960

6061
def _send(self, repo, text, check_repo: Callable[[Repo], bool], suffix=REPLY_MESSAGE):
61-
text = truncate(text, TRUNCATED_MESSAGE, suffix)
62+
truncated_text = {}
6263

63-
for chat_id, repo in self._iter_repos(repo):
64+
for chat_id, chat_data, repo in self._iter_repos(repo):
6465
if check_repo(repo):
66+
truncation_limit = chat_data.get('truncation_limit', DEFAULT_TRUNCATION_LIMIT)
67+
try:
68+
text = truncated_text[truncation_limit]
69+
except KeyError:
70+
text = truncate(text, TRUNCATED_MESSAGE, suffix, max_length=truncation_limit)
71+
6572
self.dispatcher.bot.send_message(chat_id=chat_id, text=text,
6673
parse_mode=ParseMode.HTML, disable_web_page_preview=True)
6774

bot/settings.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from telegram import Chat, InlineQueryResultArticle, InputTextMessageContent, ParseMode
55
from telegram.ext import Dispatcher, InlineQueryHandler, CommandHandler
66

7+
from bot.const import DEFAULT_TRUNCATION_LIMIT
78
from bot.github import github_api
89
from bot.menu import Button, Menu, BackButton, reply_menu, MenuHandler, ToggleButton, SetButton
910
from bot.repo import Repo
@@ -56,8 +57,9 @@ def settings_buttons(update, context):
5657
else:
5758
buttons.append(Button('🔑 Login', menu='login'))
5859
else:
59-
buttons.append(Button('👤 User settings', url=f'https://telegram.me/{context.bot.username}?start=settings'))
60+
buttons.append(Button('👤 Go to User settings', url=f'https://telegram.me/{context.bot.username}?start=settings'))
6061

62+
buttons.append(Button('👥 Chat settings', menu='chat'))
6163
buttons.append(Button('🗃️ Repositories', menu='repos'))
6264

6365
return [[button] for button in buttons]
@@ -172,6 +174,39 @@ def repo_set_data(update, context):
172174
)
173175

174176

177+
def chat_text(update, context):
178+
if update.effective_chat.title:
179+
chat = update.effective_chat.title
180+
elif update.effective_chat.first_name:
181+
chat = f'this chat with {update.effective_chat.first_name}'
182+
else:
183+
chat = 'this chat'
184+
return f'⚙ Settings for {context.bot.name} for {chat}\n\n'
185+
186+
187+
def chat_buttons(update, context):
188+
truncation_limit = context.chat_data.get('truncation_limit', DEFAULT_TRUNCATION_LIMIT)
189+
truncation_limits = [256, 512, 1024, 2048, 4096]
190+
truncation_limit_states = [(limit, f'Max notification message length: {limit}') for limit in truncation_limits]
191+
192+
return [
193+
[ToggleButton('truncation_limit', truncation_limit, states=truncation_limit_states)],
194+
[BackButton(BACK)]
195+
]
196+
197+
198+
def chat_set_data(update, context):
199+
context.chat_data[context.key] = context.value
200+
201+
202+
chat_settings_menu = Menu(
203+
name='chat',
204+
text=chat_text,
205+
buttons=chat_buttons,
206+
set_data=chat_set_data
207+
)
208+
209+
175210
def settings_command(update, context):
176211
if context.args:
177212
context.menu_stack = context.args
@@ -265,7 +300,8 @@ def add_handlers(dp: Dispatcher):
265300
dp.add_handler(MenuHandler(settings_menu, [
266301
repos_menu,
267302
repo_menu,
268-
login_menu
303+
login_menu,
304+
chat_settings_menu
269305
]))
270306

271307
dp.add_handler(InlineQueryHandler(inline_add_repo, pattern=InlineQueries.add_repo + r'(.*)'))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy