Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format text converter channels #1709

Merged

Conversation

hasinaxp
Copy link
Contributor

@hasinaxp hasinaxp commented Dec 27, 2024

whatsapp, telegram, messenger & instagram

Summary by CodeRabbit

  • New Features

    • Added support for formatted text messages across Messenger, Telegram, and WhatsApp.
    • Introduced a new message type, formatText, in the messaging template for various platforms.
  • Bug Fixes

    • Enhanced error handling in message processing methods for better reliability.
  • Tests

    • Expanded test coverage for the new paragraph transformation functionality in the response converters for WhatsApp, Telegram, and Messenger.

Copy link

coderabbitai bot commented Dec 27, 2024

Walkthrough

The pull request introduces a new feature for handling formatted text messages across multiple messaging platforms (Messenger, Telegram, WhatsApp). A new paragraph_transformer method is added to response converters for each platform, enabling the processing of text with formatting like bold, italic, and strikethrough. The changes include updating message converters to support the new FORMAT_TEXT element type, modifying channel handlers to recognize the new message format, and expanding the message template configuration to accommodate formatted text messages.

Changes

File Change Summary
kairon/shared/constants.py Added new FORMAT_TEXT enumeration member
kairon/chat/converters/channels/messenger.py Added paragraph_transformer method, updated messageConverter
kairon/chat/converters/channels/telegram.py Added paragraph_transformer method
kairon/chat/converters/channels/whatsapp.py Added paragraph_transformer method, updated messageConverter
kairon/chat/handlers/channels/telegram.py Modified send_custom_json to handle formatText type
kairon/chat/handlers/channels/whatsapp.py Updated content_type dictionary with formatText
metadata/message_template.yml Added formatText entry for multiple messaging platforms
tests/unit_test/utility_test.py Added comprehensive test methods for paragraph transformers

Sequence Diagram

sequenceDiagram
    participant User
    participant MessageHandler
    participant ResponseConverter
    participant ChannelSpecificHandler

    User->>MessageHandler: Send formatted text message
    MessageHandler->>ResponseConverter: Convert message
    ResponseConverter->>ResponseConverter: paragraph_transformer
    ResponseConverter-->>ChannelSpecificHandler: Formatted message
    ChannelSpecificHandler->>User: Deliver message
Loading

Poem

🐰 A rabbit's tale of text so bright,
Formatting words with pure delight!
Bold and italic, no longer plain,
Across channels, our messages reign!
Hop-tastic updates, code so clean! 🌈

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
kairon/shared/constants.py (1)

129-129: Consider aligning naming conventions for the new enum member.

Currently, "formatText" uses partial camel case, whereas other enum values are lower-case (e.g., "link", "image", "quick_reply"). For consistency, you could rename it to "format_text".

- FORMAT_TEXT = "formatText"
+ FORMAT_TEXT = "format_text"
kairon/chat/converters/channels/telegram.py (1)

39-66: Add exception chaining for clarity in paragraph_transformer.

On line 65, consider chaining the raised exception via raise ... from ex to precisely distinguish it from errors in the exception handling block.

- raise Exception(f"Error in TelegramResponseConverter::paragraph_transformer {str(ex)}")
+ raise Exception(f"Error in TelegramResponseConverter::paragraph_transformer {str(ex)}") from ex
🧰 Tools
🪛 Ruff (0.8.2)

65-65: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

kairon/chat/converters/channels/messenger.py (1)

39-57: Add exception chaining for clarity in paragraph_transformer.

On line 56, consider chaining the raised exception via raise ... from ex to help identify its origin more accurately.

- raise Exception(f"Error in MessengerResponseConverter::paragraph_transformer {str(ex)}")
+ raise Exception(f"Error in MessengerResponseConverter::paragraph_transformer {str(ex)}") from ex
🧰 Tools
🪛 Ruff (0.8.2)

56-56: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

kairon/chat/converters/channels/whatsapp.py (1)

37-64: Add exception chaining for clarity in paragraph_transformer.

On line 63, chaining the raised exception with from ex can help pinpoint the original exception more precisely.

- raise Exception(f"Error in WhatsappResponseConverter::paragraph_transformer {str(ex)}")
+ raise Exception(f"Error in WhatsappResponseConverter::paragraph_transformer {str(ex)}") from ex
🧰 Tools
🪛 Ruff (0.8.2)

63-63: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7478f83 and b931a5d.

📒 Files selected for processing (8)
  • kairon/chat/converters/channels/messenger.py (3 hunks)
  • kairon/chat/converters/channels/telegram.py (3 hunks)
  • kairon/chat/converters/channels/whatsapp.py (3 hunks)
  • kairon/chat/handlers/channels/telegram.py (1 hunks)
  • kairon/chat/handlers/channels/whatsapp.py (1 hunks)
  • kairon/shared/constants.py (1 hunks)
  • metadata/message_template.yml (4 hunks)
  • tests/unit_test/utility_test.py (3 hunks)
🧰 Additional context used
🪛 yamllint (1.35.1)
metadata/message_template.yml

[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)

🪛 Ruff (0.8.2)
kairon/chat/converters/channels/telegram.py

65-65: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

kairon/chat/converters/channels/messenger.py

56-56: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

kairon/chat/converters/channels/whatsapp.py

63-63: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🔇 Additional comments (24)
kairon/chat/converters/channels/telegram.py (1)

98-99: The FORMAT_TEXT branch looks good.

The additional condition for FORMAT_TEXT in messageConverter cleanly delegates processing to paragraph_transformer.

kairon/chat/converters/channels/messenger.py (1)

113-114: The FORMAT_TEXT handling is properly integrated.

Good job adding the condition for FORMAT_TEXT to delegate processing to paragraph_transformer.

kairon/chat/converters/channels/whatsapp.py (1)

152-153: FORMAT_TEXT case is appropriately handled.

Your additional condition in messageConverter properly calls paragraph_transformer for formatted text.

kairon/chat/handlers/channels/telegram.py (1)

159-159: Add support for 'formatText' in Telegram.

This condition properly includes 'formatText' alongside 'link' and 'video'. The logic appends the 'text' field to response_list and invokes the same send_message method used for regular text messages.

kairon/chat/handlers/channels/whatsapp.py (1)

236-236: Extend content types to include 'formatText' for WhatsApp.

Adding "formatText": "text" in the content_type dictionary ensures the WhatsApp channel can handle formatted text. This is consistent with the new formatText type introduced in the codebase.

tests/unit_test/utility_test.py (15)

3339-3357: Test basic paragraph transformation for WhatsApp.

These lines introduce a test verifying the transformation of a single paragraph of text into WhatsApp’s JSON structure. This ensures straightforward text is appended with a trailing newline.


3358-3375: Test paragraph transformation with bold text for WhatsApp.

This test confirms that bold text is correctly wrapped with asterisks (*text*) in the final WhatsApp message body. The fallback for normal text remains unaltered.


3376-3392: Test leading/trailing spaces in paragraph transformation for WhatsApp.

Ensures that user-provided spacing is preserved in the final message by maintaining exact leading and trailing whitespaces.


3393-3402: Test empty paragraphs for WhatsApp.

Checks converting an empty list of paragraphs produces an empty body. This prevents errors in scenarios where no text is supplied.


3403-3412: Test exception handling in WhatsApp paragraph transformation.

Here, the test verifies that custom exceptions are raised and includes proper error messages when underlying configuration lookups fail.


3414-3428: Test basic paragraph transformation for Telegram.

Examines the function’s correctness when converting a single paragraph. Ensures a trailing newline is appended for a clean read in Telegram messages.


3429-3444: Test bold text transformation for Telegram.

Checks that bold property in the object is translated to HTML tags (<b>...</b>) for Telegram. Normal text remains unaffected.


3445-3459: Test leading/trailing spaces for Telegram paragraphs.

Verifies any existing whitespace is left intact in the final content. This helps preserve user-intended formatting.


3460-3467: Test empty paragraphs for Telegram.

Ensures an empty array of paragraphs cleanly produces an empty "text" field, preventing any extraneous newlines.


3468-3475: Test exception handling in Telegram paragraph formatting.

This block validates correct exception propagation if the channel configuration fails to load or parse.


3477-3491: Test basic paragraph transformation for Messenger.

Ensures a single paragraph is handled correctly in Messenger with a trailing newline.


3492-3507: Test bold text transformation in Messenger paragraphs.

Confirms that bold text simply remains standard text without formatting markup, matching current Messenger design.


3508-3522: Test leading/trailing spaces in Messenger paragraphs.

Checks that user-provided spacing is retained, preserving the original text spacing.


3523-3530: Test empty paragraphs for Messenger.

Verifies that an empty structure leads to an empty "text" field with no appended newline.


3531-3537: Test exception handling in Messenger paragraph transformation.

Ensures that exceptions are thrown with the correct context and message when channel configuration retrieval fails.

metadata/message_template.yml (4)

1-1: Add 'formatText' to type_list.

This new element type enables rendering styled text segments across all supported channels.

🧰 Tools
🪛 yamllint (1.35.1)

[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


[warning] 1-1: too few spaces after comma

(commas)


91-91: Add 'formatText' entry for Messenger.

The JSON template extends Messenger’s support to handle custom formatted text via a simple "text" field.


116-119: Introduce 'formatText' template for WhatsApp.

Defines "preview_url" and "body" fields to capture styled text in a single message body, aligning with the newly supported formatText element.


136-142: Include 'formatText' in Telegram’s message template.

The block declares how Telegram should display formatted text, applying "parse_mode": "HTML" for styling.

Copy link
Collaborator

@sushantpatade sushantpatade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

@sushantpatade sushantpatade merged commit 515c6a1 into digiteinfotech:master Dec 27, 2024
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants