Dify
Send, receive, and answer Photon messages in Dify workflows, agents, and chat apps with Photon's Dify plugins.
Photon publishes three plugins on the Dify 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 | Tool | Create or get users and Spaces, send messages, and read Spaces, messages, users, and attachments, from a workflow, chatflow, or agent. |
| Photon Trigger | Trigger | Start a workflow when the project receives a message. |
| 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_messagestep. See 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_messageto reply. For example, look up an order when a message mentions a refund, then answer. See 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.
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 are maintained by Photon.
- Set up a Beta project and API key, 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 withoutBearer. 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. - Connect the channels you want to use in the Beta dashboard. Start with the iMessage or email guide.
- 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_URLto an address of Dify's API that the plugin can reach, for examplehttp://api:5001in Docker. To send an image uploaded to or generated in Dify, the Photon plugin first downloads it from a file URL that Dify builds fromINTERNAL_FILES_URL, or fromFILES_URLif 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_URLto the public HTTPS address of Dify's API, for examplehttps://dify.example.com. Before you copy a Photon Bot endpoint's URL, setENDPOINT_URL_TEMPLATEto a public HTTPS URL that keeps{hook_id}, for examplehttps://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, after Upload attachment for each image |
list_spaces | Lists Spaces (conversations), optionally for one platform or participant phone number or email address. | List Spaces |
create_or_get_space | Creates or gets a Space on one platform for the given users. | Create or get a Space |
list_messages | Lists messages in the project, optionally for one platform. To list one Space's messages, pass space_id and your Agent User as perspective_user_id. | List Messages |
get_message | Gets one message, including its delivery status. | Get a Message |
get_user | Gets one user, including its handle and whether it is an agent. | Get a User |
create_or_get_user | Creates or gets the user for a phone number or email address on one platform. | Create or get a User |
get_attachment | Gets an attachment's metadata and file. | Get attachment, then Download attachment |
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:
- Get or create the users. Call
create_or_get_userwith the platform and the person's phone number (E.164, for example+14155550123) or email address ashandle. Then call it with your own number or address. Photon recognizes it as yours and returns your Agent User, whosekindisagent. - Create the Space. Call
create_or_get_spaceon the same platform, with both user IDs asparticipant_ids, separated by a comma. - Send messages. Call
send_messagewith the Space's ID asspace_idand your Agent User assender_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. - Email: Each
create_or_get_spacecall without anidempotency_key, or with a new one, starts a new email thread. - Telegram:
create_or_get_userandcreate_or_get_spacefail with422OPERATION_UNAVAILABLEon 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
typeisgroup; - an edit, whose content
typeismessage.update, with the new content incontent.content.
The trigger receives events through a Photon webhook destination. 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
Difythat sendsmessage.receivedevents, with API version2026-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
with the subscription's endpoint URL, API version
2026-07-01, and themessage.receivedevent. Then enter itswhsec_...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.
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.
Add a webhook destination
Add a webhook destination
with the endpoint's URL, API version 2026-07-01, and the
message.received event. Copy its whsec_... signing secret.
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.