Skip to content

Migrate send message serverless endpoints into lambdas/account-scoped#3973

Merged
stephenhand merged 5 commits intomasterfrom
copilot/migrate-send-message-endpoints
Mar 5, 2026
Merged

Migrate send message serverless endpoints into lambdas/account-scoped#3973
stephenhand merged 5 commits intomasterfrom
copilot/migrate-send-message-endpoints

Conversation

Copy link
Contributor

Copilot AI commented Feb 27, 2026

Ports three Twilio Serverless functions (sendSystemMessage, sendStudioMessage, sendMessageAndRunJanitor) into lambdas/account-scoped/src/conversation/, following the same migration patterns as getTaskAndReservations. Adds a feature flag in plugin-hrm-form to gate the new lambda endpoint for sendSystemMessage.

Description

Lambda changes (lambdas/account-scoped)

  • conversation/sendSystemMessage.ts — Sends a system message to a conversation, chat channel, or a task's associated channel. Replaces context.* env vars with getChatServiceSid/getWorkspaceSid lookups; returns Result<HttpError, any> instead of serverless callback. Registered with validateFlexTokenRequest({ tokenMode: 'agent' }) (was TokenValidator-wrapped).

  • conversation/sendStudioMessage.ts — Removes the studio webhook on a channel before calling sendSystemMessage to prevent Studio re-triggering. Registered with validateWebhookRequest (was .protected.ts).

  • conversation/sendMessageAndRunJanitor.ts — Removes studio webhooks, sends a system message, then runs chatChannelJanitor to close the conversation/channel. Supports both conversationSid and channelSid paths. Registered with validateWebhookRequest (was .protected.ts).

  • router.ts — Adds three new routes under conversation/sendSystemMessage, conversation/sendStudioMessage, and conversation/sendMessageAndRunJanitor.

  • Unit tests added for all three handlers covering validation errors, happy paths, and error propagation.

  • Logging — Replaced console.log calls with appropriate log level methods (console.debug for entry points and verbose tracing, console.info for key operations/milestones, console.error for unexpected failures). Added extra debug and info logs to all three handlers to aid debugging in production.

Plugin changes (plugin-hrm-form)

  • types/FeatureFlags.ts — Added use_twilio_lambda_to_send_messages feature flag.

  • services/twilioConversationService.ts (new file) — Contains sendSystemMessage, moved here from ServerlessService.ts. When use_twilio_lambda_to_send_messages is enabled, calls the migrated lambda endpoint (/conversation/sendSystemMessage); otherwise falls back to the legacy serverless endpoint (/sendSystemMessage).

  • services/ServerlessService.ts — Removed sendSystemMessage (moved to twilioConversationService.ts).

  • utils/setUpActions.ts — Updated import of sendSystemMessage to use the new twilioConversationService.

Checklist

  • Corresponding issue has been opened
  • New tests added
  • Feature flags added
  • Strings are localized
  • Tested for chat contacts
  • Tested for call contacts

Other Related Issues

None

Verification steps

Deploy and invoke each endpoint via the account-scoped Lambda path:

  • POST /lambda/twilio/account-scoped/{accountSid}/conversation/sendSystemMessage with a valid Flex token and a conversationSid/channelSid/taskSid + message
  • POST /lambda/twilio/account-scoped/{accountSid}/conversation/sendStudioMessage with a Twilio-signed request and a channelSid + message
  • POST /lambda/twilio/account-scoped/{accountSid}/conversation/sendMessageAndRunJanitor with a Twilio-signed request and a conversationSid or channelSid + message — verify the conversation/channel is deactivated after the call

To test the feature flag routing in plugin-hrm-form:

  • Set use_twilio_lambda_to_send_messages: true in the service configuration feature flags and verify sendSystemMessage calls route to the lambda endpoint
  • With the flag unset or false, verify sendSystemMessage calls route to the legacy serverless endpoint

AFTER YOU MERGE

  1. Cut a release tag using the Github workflow. Wait for it to complete and notify in the #aselo-deploys Slack channel.
  2. Comment on the ticket with the release tag version AND any additional instructions required to configure an environment to test the changes.
  3. Only then move the ticket into the QA column in JIRA

You are responsible for ensuring the above steps are completed. If you move a ticket into QA without advising what version to test, the QA team will assume the latest tag has the changes. If it does not, the following confusion is on you! :-P

Original prompt

This section details on the original issue you should resolve

<issue_title>Migrate the send message serverless endpoints into the lambdas/account-scoped</issue_title>
<issue_description>## Description

Migrate the following 3 twilio serverless endpoints into the lambdas/account-scoped

https://github.com/techmatters/serverless/blob/master/functions/sendMessageAndRunJanitor.ts
https://github.com/techmatters/serverless/blob/master/functions/sendStudioMessage.ts
https://github.com/techmatters/serverless/blob/master/functions/sendSystemMessage.ts

Place these in the conversation folder

Additional context

Follow the same patterns as for other endpoints that have been migrated

For example, https://github.com/techmatters/serverless/blob/master/functions/getTaskAndReservations.ts was recently migrated to https://github.com/techmatters/flex-plugins/blob/master/lambdas/account-scoped/src/task/getTaskAndReservations.ts

Note how when a TokenValidator wrapper is used in the serverless version, a validateFlexTokenRequest item needs to be added to the request pipeline for the route in account-scoped
</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…onversation folder

Co-authored-by: stephenhand <1694716+stephenhand@users.noreply.github.com>
Copilot AI changed the title [WIP] Migrate send message serverless endpoints into lambdas Migrate send message serverless endpoints into lambdas/account-scoped Feb 27, 2026
Copy link
Collaborator

@stephenhand stephenhand left a comment

Choose a reason for hiding this comment

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

Fix the linter error raised in the last CI run

Co-authored-by: stephenhand <1694716+stephenhand@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 27, 2026

Fix the linter error raised in the last CI run

Fixed in commit 1640356. Prettier required a line break in the long error message string in sendSystemMessage.ts.

Copy link
Collaborator

@stephenhand stephenhand left a comment

Choose a reason for hiding this comment

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

Uppdate the existing logs and add extra logs in this PR to aid debugging in accordance with the coding standards. Replace console.log calls with methods appropriate to the level the logs should be

Co-authored-by: stephenhand <1694716+stephenhand@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 2, 2026

Uppdate the existing logs and add extra logs in this PR to aid debugging in accordance with the coding standards. Replace console.log calls with methods appropriate to the level the logs should be

Done in commit 8f88a4e. Updated all three handlers:

  • sendSystemMessage.ts: console.log entry banner → console.debug with params; action logs → console.info with "system message" prefix; added console.error in catch
  • sendStudioMessage.ts: added console.debug entry log, console.debug before webhook removal, console.info after removal, console.error in catch
  • sendMessageAndRunJanitor.ts: added console.debug entry log, console.debug before webhook removal, console.info after removal, console.error in catch

Security Summary: No security vulnerabilities found (CodeQL scan returned 0 alerts).

@stephenhand stephenhand marked this pull request as ready for review March 3, 2026 09:17
Copy link
Collaborator

@stephenhand stephenhand left a comment

Choose a reason for hiding this comment

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

Add a feature flag to plugin-hrm-form called use_twilio_lambda_to_send_messages

Use this flag to determine whether the sendSystemMessage method in ServerlessService.ts should send to the legacy endpoint or the migrated endpoint. It should send to the migrated endpoint if set

Move sendSystemMessage our of ServerlessService.ts to a new twilioConversationService.ts file in the same directory

…emMessage to twilioConversationService.ts

Co-authored-by: stephenhand <1694716+stephenhand@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 4, 2026

Add a feature flag to plugin-hrm-form called use_twilio_lambda_to_send_messages

Use this flag to determine whether the sendSystemMessage method in ServerlessService.ts should send to the legacy endpo...

Done in commit f36deb8:

  • Added use_twilio_lambda_to_send_messages to FeatureFlags.ts (in alphabetical order, after the other use_twilio_lambda_for_* flags)
  • Created plugin-hrm-form/src/services/twilioConversationService.ts with sendSystemMessage that routes to /conversation/sendSystemMessage on the account-scoped lambda when the flag is set, or /sendSystemMessage on the legacy serverless endpoint otherwise
  • Removed sendSystemMessage from ServerlessService.ts
  • Updated the import in setUpActions.ts to use twilioConversationService

@stephenhand stephenhand merged commit d58cedb into master Mar 5, 2026
52 checks passed
@stephenhand stephenhand deleted the copilot/migrate-send-message-endpoints branch March 5, 2026 09:14
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.

Migrate the send message serverless endpoints into the lambdas/account-scoped

2 participants