> **Photon Beta documentation**
> Browse all pages: https://photon.codes/docs/beta/llms.txt
> Search Beta docs via MCP: https://photon.codes/docs/beta/mcp (OAuth REST API MCP: https://mcp.photon.codes/mcp)

# Dify

> Send, receive, and answer Photon messages in Dify workflows, agents, and chat apps with Photon's Dify plugins.

> [!WARNING]
>
> **Beta is a preview.** The Beta (2.0) platform does not have all the features
> of [Stable](https://photon.codes/docs/spectrum-ts/introduction) yet. For example, iMessage on Beta
> supports text, attachments, voice messages, replies, read receipts, and typing
> indicators, but tapbacks, polls, and message effects are
> [only on Stable](https://photon.codes/docs/spectrum-ts/providers/imessage/messaging-features) for now.
> Use Stable for production agents, and confirm that Beta supports the features
> you need before you buy a plan.

Photon publishes three plugins on the [Dify](https://dify.ai) Marketplace. They
connect Dify apps to the channels connected to a Photon Beta project: iMessage,
SMS, WhatsApp, WhatsApp Business, email, and Telegram.

| Plugin | Type | Use it to |
| - | - | - |
| [Photon](https://marketplace.dify.ai/plugin/photon-hq/photon) | Tool | Create or get users and Spaces, send messages, and read Spaces, messages, users, and attachments, from a workflow, chatflow, or agent. |
| [Photon Trigger](https://marketplace.dify.ai/plugin/photon-hq/photon_trigger) | Trigger | Start a workflow when the project receives a message. |
| [Photon Bot](https://marketplace.dify.ai/plugin/photon-hq/photon_bot) | Extension (endpoint) | Answer received text messages with a Chatflow or Chatbot app. |

## Choose a plugin

* **Reply automatically with a chat app.** Use Photon Bot. It sends the text
  messages the project receives to a Dify Chatflow or Chatbot app, and sends the
  app's answer back in the same conversation. You don't need a trigger or a
  `send_message` step. See
  [Answer messages with a chat app](#answer-messages-with-a-chat-app).
* **Run your own workflow when a message arrives.** Use Photon Trigger to start
  the workflow, with the received message as its variables, and the Photon
  plugin's `send_message` to reply. For example, look up an order when a message
  mentions a refund, then answer. See
  [Start workflows from received messages](#start-workflows-from-received-messages).
* **Send messages or read conversations from a workflow or agent.** Use the
  Photon plugin's tools, for example to message a customer from a scheduled
  workflow. See [Send and read messages](#send-and-read-messages).

## Before you start

This guide covers version **0.1.2** of each plugin, which requires Dify 1.17.1
or later. The [source and issue tracker](https://github.com/photon-hq/dify) are
maintained by Photon.

1. [Set up a Beta project and API key](/docs/beta/get-started/project.md), and copy the
   project ID (`pho_prj_...`). The plugins accept a project API key
   (`pho_sk_...`) or an account service key (`pho_ask_...`). Enter the key
   without `Bearer`. A project API key must belong to the project you configure.
   Photon Bot has no project setting: it replies in the project named in each
   event, so a project API key must belong to the project whose messages it
   answers.
2. Connect the channels you want to use in the
   [Beta dashboard](https://app.beta.photon.codes). Start with the
   [iMessage](/docs/beta/get-started/imessage.md) or [email](/docs/beta/get-started/email.md) guide.
3. Install the plugins you need from the Marketplace links above.

A project API key needs these permissions. An account service key has no
permission list; it has the same permissions as its account.

| Plugin | Project API key permissions |
| - | - |
| Photon | `project:read` and `project:write` |
| Photon Trigger | `project:read` and `project:write`, when the plugin creates the webhook destination |
| Photon Bot | `project:write` |

On self-hosted Dify, check these settings before you use the plugins:

* The plugins call the Photon API at `https://api.photon.codes`, so Dify must
  be able to reach it.
* Set `INTERNAL_FILES_URL` to an address of Dify's API that the plugin can
  reach, for example `http://api:5001` in Docker. To send an image uploaded to
  or generated in Dify, the Photon plugin first downloads it from a file URL
  that Dify builds from `INTERNAL_FILES_URL`, or from `FILES_URL` if that is
  empty. Both are empty by default in Dify's Docker deployment, so the URL is
  only a path, such as `/files/...`, and the download fails.
* Photon delivers webhooks to Photon Trigger and Photon Bot over HTTPS, at URLs
  that Dify builds from its settings. Before you create a trigger subscription,
  set `TRIGGER_URL` to the public HTTPS address of Dify's API, for example
  `https://dify.example.com`. Before you copy a Photon Bot endpoint's URL, set
  `ENDPOINT_URL_TEMPLATE` to a public HTTPS URL that keeps `{hook_id}`, for
  example `https://dify.example.com/e/{hook_id}`.

## Send and read messages

Authorize the **Photon** plugin with your API key and project ID. Dify checks
them by listing one Space. The plugin provides these tools:

| Tool | What it does | API |
| - | - | - |
| `send_message` | Sends a message into an existing Space. | [Send a Message](/docs/beta/api-reference/messages/send-a-message.md), after [Upload attachment](/docs/beta/api-reference/attachments/upload-attachment.md) for each image |
| `list_spaces` | Lists Spaces (conversations), optionally for one platform or participant phone number or email address. | [List Spaces](/docs/beta/api-reference/spaces/list-spaces.md) |
| `create_or_get_space` | Creates or gets a Space on one platform for the given users. | [Create or get a Space](/docs/beta/api-reference/spaces/create-or-get-a-space.md) |
| `list_messages` | Lists messages in the project, optionally for one platform. To list one Space's messages, pass `space_id` and your [Agent User](#start-a-conversation) as `perspective_user_id`. | [List Messages](/docs/beta/api-reference/messages/list-messages.md) |
| `get_message` | Gets one message, including its delivery `status`. | [Get a Message](/docs/beta/api-reference/messages/get-a-message.md) |
| `get_user` | Gets one user, including its handle and whether it is an agent. | [Get a User](/docs/beta/api-reference/users/get-an-user.md) |
| `create_or_get_user` | Creates or gets the user for a phone number or email address on one platform. | [Create or get a User](/docs/beta/api-reference/users/create-or-get-a-user.md) |
| `get_attachment` | Gets an attachment's metadata and file. | [Get attachment](/docs/beta/api-reference/attachments/get-attachment.md), then [Download attachment](/docs/beta/api-reference/attachments/download-attachment.md) |

Each tool returns Photon's JSON response and also exposes each of its top-level
fields as an output variable.

`send_message` and `create_or_get_space` also return the `idempotency_key` they
sent: yours, or a UUID they generated when you left it empty. To retry a failed
call without sending twice, call the tool again with the same key and the same
parameters. If you left the key empty and the call to Photon fails, the error
message includes the generated key.

`get_attachment` also returns the file as a Dify file output, for example to
pass an image to a vision model in a workflow. If the attachment is larger than
50 MB, the tool fails with an error and returns neither the file nor its
metadata.

### Start a conversation

In Photon, a conversation is a Space, and everyone in it is a user
(`pho_usr_...`), including you: the phone number, email address, or bot that
your project sends from is a user called your Agent User. To start a
conversation:

1. **Get or create the users.** Call `create_or_get_user` with the platform
   and the person's phone number (E.164, for example `+14155550123`) or email
   address as `handle`. Then call it with your own number or address. Photon
   recognizes it as yours and returns your Agent User, whose `kind` is `agent`.
2. **Create the Space.** Call `create_or_get_space` on the same platform, with
   both user IDs as `participant_ids`, separated by a comma.
3. **Send messages.** Call `send_message` with the Space's ID as `space_id` and
   your Agent User as `sender_id`.

Some platforms have their own rules:

* **iMessage**: List your Agent User first in `participant_ids`. Only a
  dedicated line can start a new conversation, not a
  [shared line](/docs/beta/get-started/imessage.md).
* **Email**: Each `create_or_get_space` call without an `idempotency_key`, or
  with a new one, starts a new email thread.
* **Telegram**: `create_or_get_user` and `create_or_get_space` fail with `422`
  `OPERATION_UNAVAILABLE` on Telegram, even for users and Spaces that already
  exist. The conversation starts when the person messages your bot.

## Start workflows from received messages

The **Photon Trigger** plugin's **Message received** trigger starts a workflow
when the project receives a message (the `message.received` event). The
trigger's variables are the received Photon message, including `platform`,
`space`, `sentBy`, `content`, and, when present, `receivedBy`. To reply, pass
`space.id` as `space_id` and `receivedBy.id` as `sender_id` to the Photon
plugin's `send_message`. On iMessage, a group message can arrive without
`receivedBy`. Then send as your line's Agent User: it is one of
`space.participantIds`, and `get_user` shows its `kind` as `agent`.

You can limit which messages start the workflow:

| Parameter | Starts the workflow only for |
| - | - |
| Platforms | Messages on the selected platforms. Leave empty for all platforms. |
| Text contains | Messages whose text contains this value, ignoring case. Messages without text are skipped. |
| Text regex | Messages whose text matches this Python regular expression. The match can start anywhere in the text and is case-sensitive. A message without text is matched as empty text. An invalid expression skips every message. |

Both text filters read the message's `content.text`. A message whose content
has no `text` field counts as having no text, for example:

* an attachment;
* text sent together with attachments, whose content `type` is `group`;
* an edit, whose content `type` is `message.update`, with the new content in
  `content.content`.

The trigger receives events through a Photon
[webhook destination](/docs/beta/message-delivery/webhook.md). When you create the
trigger's subscription in Dify, set the destination up in one of two ways:

* **Enter your API key and project ID.** The plugin creates a destination named
  `Dify` that sends `message.received` events, with API version `2026-07-01`,
  to the subscription's endpoint, and stores its signing secret. Deleting the
  subscription deletes the destination. If that deletion fails, Dify still
  deletes the subscription, and the destination stays in Photon.
* **Create the destination yourself.**
  [Add a webhook destination](/docs/beta/message-delivery/webhook.md#add-a-destination)
  with the subscription's endpoint URL, API version `2026-07-01`, and the
  `message.received` event. Then enter its `whsec_...` signing secret in Dify.

The trigger verifies each delivery's signature and rejects deliveries whose
timestamp is more than five minutes from the current time.

## Answer messages with a chat app

The **Photon Bot** plugin adds an endpoint that answers received text messages
with a Dify Chatflow or Chatbot app.

## 1. Add an endpoint

In the Photon Bot plugin's settings, add an endpoint. Choose the app that
answers, enter an API key with `project:write`, and leave **Signing secret**
empty. Save the endpoint and copy its URL.

## 2. Add a webhook destination

[Add a webhook destination](/docs/beta/message-delivery/webhook.md#add-a-destination)
with the endpoint's URL, API version `2026-07-01`, and the
`message.received` event. Copy its `whsec_...` signing secret.

## 3. Enter the signing secret

Edit the endpoint and enter the signing secret. Until you do, the endpoint
rejects every delivery with `401`. Photon does not retry these deliveries,
so messages that arrive before then get no answer.

For each received text message, the endpoint sends the text to the app as the
user's query and waits for the complete answer. If the answer is not empty, it
sends the answer into the message's Space as plain text, from the Agent User
that received the message. It keeps one Dify conversation per app and Space,
so every message from a Space continues the same conversation in the app.

The endpoint answers only messages whose content `type` is `text`, that were
not sent by an agent, and that have a `receivedBy` user. It acknowledges other
events and messages without answering them.

The endpoint remembers the last message it handled for each app and Space, and
records it before it calls the app. When Photon delivers that message again,
for example after a timeout or a `5xx` response, the endpoint acknowledges it
without calling the app. So if the app call or the reply fails, the message
gets no answer, even when Photon delivers it again.
