#!/usr/bin/env bash # Import and publish a sample chatflow, from https://exe.dev/deploy/dify # Run on the VM after setup-models.sh: # DIFY_EMAIL=you@example.com bash sample-app.sh helpdesk-bot.yml # It imports the app through the console on localhost:8080, publishes it, # makes a service API key, and asks one question through the apps-and-API # port, localhost:8000. Find the key later in the app's Access Point page. set -euo pipefail : "${DIFY_EMAIL:?set DIFY_EMAIL to the admin account email}" DSL=${1:?usage: sample-app.sh APP.yml} PASSWORD_FILE=${PASSWORD_FILE:-$HOME/dify-admin-password} CONSOLE=http://localhost:8080/console/api jar=$(mktemp) trap 'rm -f "$jar"' EXIT api() { local method=$1 path=$2; shift 2 curl -fsS -X "$method" -b "$jar" -H 'Content-Type: application/json' \ -H "X-CSRF-Token: $(awk '$6 ~ /csrf_token$/ {print $7}' "$jar")" \ "$CONSOLE$path" "$@" } password=$(tr -d '\n' < "$PASSWORD_FILE" | base64 -w0) curl -fsS -c "$jar" -H 'Content-Type: application/json' "$CONSOLE/login" \ -d "{\"email\":\"$DIFY_EMAIL\",\"password\":\"$password\",\"remember_me\":true}" >/dev/null result=$(jq -n --rawfile yaml "$DSL" '{mode: "yaml-content", yaml_content: $yaml}' \ | api POST /apps/imports -d @-) if [ "$(jq -r .status <<<"$result")" = pending ]; then result=$(api POST "/apps/imports/$(jq -r .id <<<"$result")/confirm") fi app=$(jq -r '.app_id // empty' <<<"$result") [ -n "$app" ] || { echo "Import failed: $result"; exit 1; } api POST "/apps/$app/workflows/publish" -d '{}' >/dev/null key=$(api POST "/apps/$app/api-keys" | jq -r .token) code=$(api GET "/apps/$app" | jq -r .site.access_token) echo "App: $app" echo "Web app: https://$(hostname).exe.xyz/chat/$code" echo "Asking it a question on port 8000..." curl -fsS http://localhost:8000/v1/chat-messages \ -H "Authorization: Bearer $key" -H 'Content-Type: application/json' \ -d '{"inputs":{},"query":"How long do I have to return a widget?","response_mode":"blocking","user":"setup-check"}' \ | jq -r .answer