Dify定期実行

Difyで定期実行、定期的に通知したいなど
完成イメージ
指定時刻に毎日送られてくるチャットボットなど
準備
- Difyのワークフローで指定したチャットに送信されるもの(チャットフローではなくワークフローが良いです。)
- GItHubアカウント

完成版DSL
app:
description: ''
icon: 🤖
icon_background: '#FFEAD5'
mode: workflow
name: Teamsへの通知
use_icon_as_answer_icon: false
kind: app
version: 0.1.5
workflow:
conversation_variables: []
environment_variables: []
features:
file_upload:
allowed_file_extensions:
- .JPG
- .JPEG
- .PNG
- .GIF
- .WEBP
- .SVG
allowed_file_types:
- image
allowed_file_upload_methods:
- local_file
- remote_url
enabled: false
fileUploadConfig:
audio_file_size_limit: 50
batch_count_limit: 5
file_size_limit: 15
image_file_size_limit: 10
video_file_size_limit: 100
workflow_file_upload_limit: 10
image:
enabled: false
number_limits: 3
transfer_methods:
- local_file
- remote_url
number_limits: 3
opening_statement: ''
retriever_resource:
enabled: true
sensitive_word_avoidance:
enabled: false
speech_to_text:
enabled: false
suggested_questions: []
suggested_questions_after_answer:
enabled: false
text_to_speech:
enabled: false
language: ''
voice: ''
graph:
edges:
- data:
isInIteration: false
sourceType: start
targetType: http-request
id: 1738930406772-source-1738930420056-target
source: '1738930406772'
sourceHandle: source
target: '1738930420056'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: http-request
targetType: end
id: 1738930420056-source-1738930655719-target
source: '1738930420056'
sourceHandle: source
target: '1738930655719'
targetHandle: target
type: custom
zIndex: 0
nodes:
- data:
desc: ''
selected: true
title: 開始
type: start
variables:
- label: teamspost_body
max_length: 48
options: []
required: true
type: text-input
variable: teamspost_body
height: 90
id: '1738930406772'
position:
x: 80
y: 282
positionAbsolute:
x: 80
y: 282
selected: true
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
authorization:
config: null
type: no-auth
body:
data:
- id: key-value-4
key: ''
type: text
value: "{\n \"text\": \"{{#1738930406772.teamspost_body#}}\"\n}"
type: json
desc: ''
headers: ''
method: post
params: ''
retry_config:
max_retries: 3
retry_enabled: true
retry_interval: 100
selected: false
timeout:
max_connect_timeout: 0
max_read_timeout: 0
max_write_timeout: 0
title: HTTPリクエスト
type: http-request
url: ''
variables: []
height: 280
id: '1738930420056'
position:
x: 384
y: 282
positionAbsolute:
x: 384
y: 282
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
desc: ''
outputs: []
selected: false
title: 終了
type: end
height: 54
id: '1738930655719'
position:
x: 688
y: 282
positionAbsolute:
x: 688
y: 282
sourcePosition: right
targetPosition: left
type: custom
width: 244
viewport:
x: 176.35384083880115
y: -64.01086954437324
zoom: 0.8374064875084266

1. DifyのAPIキー取得
Difyではワークフローなどを外部から実行する仕組みがあります。
左のサイドメニューからAPIアクセスへ
右上のAPIキー
> 新しいシークレットキーを作成
キーが作成されるのでコピーします。

2. GitHub Actionsで定期実行 最初の設定
何個かやり方ありますが、GitHub Actionsがやりやすいです。
2-1. フォーク
- こちらにリポジトリにアクセス
https://github.com/leochen-g/dify-schedule
このリポジトリは定期実行の仕組みを丸っと用意してくれています。これを自分のアカウントで使えるようにフォーク(コピー)します。
Create Forkボタン
で進みます。
2-2. GitHub Actionsの有効化 - 最初だけ
Actionsメニューに行きます。
許諾求められるので許諾します。
- こういう状態になればOK
2-3. DifyのAPIキーの設定
Settingsに行きます。
左のサイドメニューのSercrets and variables
> Actions
へ、
さらに、Repository sercrets
> New Repository sercret
のボタンを押す。
DIFY_TOKEN
という名前をつけて、DifyのAPIキーを設定する
確認。この状態になってればOK
DIFY_TOKENSが設定されている。

3. GitHub Actionsで定期実行 時間設定
Actionsのメニューに戻ります。
自動化処理の許可確認がまたでてるので押します。
auto.ymlというファイルを編集できるようになるので、クリックして進みます。
鉛筆マークから編集でsきます。
Cronという形式で時間設定できます。
日本時間で18:43に実行させたい時は43 9 * * *
となります。左から"分、時、日、月、曜日"です。
該当箇所を書き換えます。
ちなみに、日本時間ではなくイギリスの時間(UTC)に合わせて書かないといけないのでマイナス9時間をして記載します。Cron設定もこみでわかりにくいのでChatGPTなどに計算してもらうのがオススメです。
コミット(保存)して完了です。
実行されるのを待ちます。
あとは待ってれば良いですが、 指定時間よりも数分遅れてチャットに通知が行きます。焦らず待ってましょう。 30分くらい経っても何も無いときはトラブルが起きてるかも

Cronで定期実行もできるよ。さっきの43 9 * * *
これは"毎日18:43に実行"になっています。