Skip to content

Commit 7e8d77d

Browse files
authored
Merge pull request #13 from ionet-official/dev
deploy in main
2 parents f40d7bc + d6b3fa7 commit 7e8d77d

File tree

3 files changed

+17
-59
lines changed

3 files changed

+17
-59
lines changed

app/message_processor.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,17 @@ async def _generate_and_send_response(self, channel_id: int, user_message: Messa
116116
context_messages = context.get_context_messages()
117117
logger.debug(f"Retrieved {len(context_messages)} context messages for channel {channel_id}")
118118

119-
# Add system prompt
119+
# Add system prompt with formatting instructions
120+
formatting_instructions = ("\n\nIMPORTANT: Use only basic markdown formatting that works across platforms: "
121+
"- Use *bold* for emphasis (single asterisks work on both platforms) "
122+
"- Use `code` for inline code or technical terms "
123+
"- Use bullet points with - or • for lists "
124+
"- Use [text](url) for links "
125+
"- Avoid complex formatting, special characters, or platform-specific syntax")
126+
120127
system_prompt = {
121128
"role": "system",
122-
"content": SYSTEM_PROMPT
129+
"content": SYSTEM_PROMPT + formatting_instructions
123130
}
124131

125132
messages = [system_prompt] + context_messages

app/telegram.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
import logging
33
import time
4-
import re
54
from telegram import Update
65
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
76

@@ -12,34 +11,6 @@
1211
logger = logging.getLogger(__name__)
1312

1413

15-
def convert_markdown_to_html(text):
16-
"""Convert Discord-style markdown to Telegram HTML"""
17-
# Convert **bold** to <b>bold</b>
18-
text = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', text)
19-
20-
# Convert *italic* to <i>italic</i>
21-
text = re.sub(r'\*(.*?)\*', r'<i>\1</i>', text)
22-
23-
# Convert `code` to <code>code</code>
24-
text = re.sub(r'`(.*?)`', r'<code>\1</code>', text)
25-
26-
# Convert ```code block``` to <pre>code block</pre>
27-
text = re.sub(r'```(.*?)```', r'<pre>\1</pre>', text, flags=re.DOTALL)
28-
29-
# Escape HTML characters that aren't part of our tags
30-
# We need to be careful not to escape our intentional HTML tags
31-
text = text.replace('&', '&amp;')
32-
text = text.replace('<', '&lt;').replace('>', '&gt;')
33-
34-
# Restore our intentional HTML tags
35-
text = text.replace('&lt;b&gt;', '<b>').replace('&lt;/b&gt;', '</b>')
36-
text = text.replace('&lt;i&gt;', '<i>').replace('&lt;/i&gt;', '</i>')
37-
text = text.replace('&lt;code&gt;', '<code>').replace('&lt;/code&gt;', '</code>')
38-
text = text.replace('&lt;pre&gt;', '<pre>').replace('&lt;/pre&gt;', '</pre>')
39-
40-
return text
41-
42-
4314
class TelegramBot:
4415
"""Telegram bot implementation"""
4516

@@ -171,9 +142,8 @@ async def _handle_message(self, update: Update, context: ContextTypes.DEFAULT_TY
171142
user_mention = f"@{user.username}" if user.username else user.first_name or "User"
172143
response_text = f"{user_mention} {msg.content}"
173144

174-
# Convert markdown to HTML for better Telegram formatting
175-
html_response = convert_markdown_to_html(response_text)
176-
await update.message.reply_text(html_response, parse_mode='HTML')
145+
# Send response with simple Markdown formatting like Discord
146+
await update.message.reply_text(response_text, parse_mode='Markdown')
177147
break
178148
else:
179149
logger.error("Message processor not initialized")

tests/test_telegram.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from telegram import Update, User, Chat, Message as TelegramMessage
66
from telegram.ext import ContextTypes
77

8-
from app.telegram import TelegramBot, convert_markdown_to_html
8+
from app.telegram import TelegramBot
99
from app.models import Message
1010
from app.message_processor import MessageProcessor
1111

@@ -164,8 +164,8 @@ async def test_handle_message_success(self, telegram_bot):
164164
assert call_args[0][0] == 123 # chat_id
165165
assert isinstance(call_args[0][1], Message)
166166

167-
# Verify response was sent with user mention (since it's a group chat)
168-
update.message.reply_text.assert_called_once_with("@testuser Hi there!", parse_mode='HTML')
167+
# Verify response was sent with user mention (since it's a group chat)
168+
update.message.reply_text.assert_called_once_with("@testuser Hi there!", parse_mode='Markdown')
169169

170170
# Verify typing action was sent
171171
context.bot.send_chat_action.assert_called_once_with(chat_id=123, action="typing")
@@ -204,7 +204,7 @@ async def test_handle_message_private_chat(self, telegram_bot):
204204
await telegram_bot._handle_message(update, context)
205205

206206
# Verify response was sent without user mention (since it's a private chat)
207-
update.message.reply_text.assert_called_once_with("Hi there!", parse_mode='HTML')
207+
update.message.reply_text.assert_called_once_with("Hi there!", parse_mode='Markdown')
208208

209209
@pytest.mark.asyncio
210210
async def test_handle_message_no_text(self, telegram_bot):
@@ -496,24 +496,5 @@ async def test_handle_message_logging(self, telegram_bot):
496496
# Should log receiving message and adding to processor
497497
assert mock_logger.debug.call_count >= 2
498498

499-
def test_convert_markdown_to_html(self):
500-
"""Test markdown to HTML conversion function"""
501-
# Test bold text
502-
assert convert_markdown_to_html("**bold**") == "<b>bold</b>"
503-
504-
# Test italic text
505-
assert convert_markdown_to_html("*italic*") == "<i>italic</i>"
506-
507-
# Test code
508-
assert convert_markdown_to_html("`code`") == "<code>code</code>"
509-
510-
# Test combined formatting
511-
result = convert_markdown_to_html("**IO Net Assistant** provides *specific* info about `io.net`")
512-
expected = "<b>IO Net Assistant</b> provides <i>specific</i> info about <code>io.net</code>"
513-
assert result == expected
514-
515-
# Test HTML escaping
516-
result = convert_markdown_to_html("Test & escape < > characters")
517-
assert "&amp;" in result
518-
assert "&lt;" in result
519-
assert "&gt;" in result
499+
500+

0 commit comments

Comments
 (0)