send_bot_message
Send the customer a fixed message in an existing conversation, without an AI reply.
send_bot_message(
context: dict,
text: str,
idempotency_key: str,
conversation_id: str | None = None,
allow_handed_off: bool = False,
) -> dictSends customer-visible text right away, through the conversation's latest channel. For example, a recurring job can tell a customer that their replacement has shipped. The message is recorded as a bot-authored template message in the same conversation. It does not create a separate email thread and does not trigger an AI response.
This helper is available in recurring jobs, event handlers, and sidebar actions. It is intentionally unavailable in condition providers, system-prompt sections, sidebar widgets, and bot actions, where sending or closing while the bot is still deciding its response can create duplicate or contradictory replies.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
context | dict | Yes | The Python execution context |
text | str | Yes | Customer-visible text, from 1 to 5,000 characters |
idempotency_key | str | Yes | A stable, unique key for this logical message, from 1 to 200 characters |
conversation_id | str or None | No | Conversation UUID or public ID. Defaults to the current conversation; required in recurring jobs because they have no conversation context |
allow_handed_off | bool | No | Send even if the conversation was handed off to a human. Defaults to False; see Sending after a handoff |
Returns
{
"success": True,
"conversationId": "conversation-uuid",
"messageId": "message-uuid",
"duplicate": False,
}duplicate is True when the same idempotency key already delivered the same text to the same conversation. Reusing a key with different text or another conversation raises an HTTP conflict instead of sending anything.
Safeguards
- The conversation must belong to the organization running the Python code.
- The conversation must still be open, non-imported, and outside the phone-call channel.
- The conversation must still be bot-owned, unless you pass
allow_handed_off=True. - The transcript must not change between validation and message creation. If it does, the call fails so the automation can reevaluate the latest customer message.
- Every message requires an idempotency key. Retries after a completed delivery return the original message instead of sending another copy.
- If delivery has an ambiguous failure, Octocom does not automatically resend. This avoids showing the customer a duplicate if the channel accepted the first attempt before timing out.
- Automation bot messages are limited to 100 per organization per minute, in addition to the shared REST API limit.
- A recurring job test executes the helper normally and sends a real message. Keep a
dry_runguard enabled while testing customer-facing jobs.
Recurring job example
Use a key derived from the durable business operation, not from the job run time. That keeps retries and overlapping runs safe.
def run_job(context):
item = organization_store_get("order-cancellations", "order-123")
if not item:
return {"processed": False}
cancellation = item["value"]
conversation_id = cancellation["conversationId"]
order_id = cancellation["orderId"]
# Perform and verify the refund, package cancellation, and any required
# subscription cancellation before telling the customer it succeeded.
# Record the outcome so Bot QA can use it when checking later AI replies.
add_conversation_event(
{**context, "conversation": {"id": conversation_id}},
f"Order {order_id} cancelled and fully refunded",
)
result = send_bot_message(
context,
f"Your order {order_id} has been cancelled and fully refunded. "
"The refund will return to your original payment method.",
idempotency_key=f"order-cancelled:{order_id}:confirmation:v1",
conversation_id=conversation_id,
)
close_conversation(
context,
reason=f"Order {order_id} automatically cancelled and customer notified",
conversation_id=conversation_id,
)
return {"processed": True, "messageId": result["messageId"]}Do not close the conversation when the refund, package cancellation, subscription cancellation, or confirmation delivery fails. Leave it open for retry or manual follow-up.
Sending after a handoff
By default the helper refuses a conversation that was handed off to a human. That refusal is what stops follow-ups and nudges from talking over an agent, so many automations rely on it instead of checking the handoff themselves.
Pass allow_handed_off=True when the message must reach the customer even though a human now owns the conversation. For example, an event handler on Customer Message Sent can tell customers who write again on Instagram that the team will reply in their existing email thread:
def handle_event(context):
conversation = context["conversation"]
if not conversation.get("isHandedOff"):
return {"sent": False}
return send_bot_message(
context,
"Our team is already handling your request by email. "
"Please reply in that email thread so nothing gets lost.",
idempotency_key=f"post-handoff-email-reminder:{conversation['id']}:v1",
allow_handed_off=True,
)The other safeguards still apply: the conversation must be open, and the send fails if an agent or the customer posts a message between validation and message creation. The message appears in the Octocom conversation timeline. On email conversations synced with Help Scout, Zendesk, Gorgias or Re:amaze, it is also posted to the helpdesk ticket like any other bot reply.
Bot QA
Bot QA does not check messages sent with this helper, because they are fixed text rather than AI replies. If the AI may discuss the action later, record the outcome with add_conversation_event: QA can then use it as evidence for those replies.
Event handler example
Event handlers already have a current conversation, so conversation_id can be omitted:
def handle_event(context):
return send_bot_message(
context,
"We completed your requested account update.",
idempotency_key=f"account-update:{context['conversation']['id']}:v1",
)Related helpers
close_conversationcloses the thread after successful delivery.send_outbound_emailcreates a separate outbound email conversation instead.add_conversation_eventrecords an internal timeline event that the customer does not see.