Logo

A Step-by-Step Guide to Building a News Push Application with Dify Workflows

avatar admin 02 Dec 2024

A Step-by-Step Guide to Building a News Push Application with Dify Workflows

Introduction

In this blog, we will demonstrate how to use Dify’s workflows to build a news push application.

Our goal is to fetch the latest articles from Hacker News and push the curated news to a Feishu group. Through this process, we will experience the powerful capabilities of workflows and the convenience of automated processing.

What is a Workflow?

Before we begin, let’s briefly understand what a workflow is.

A workflow is a series of ordered tasks, activities, or steps designed to complete a specific business process or workflow. It describes the sequence of tasks, conditions, responsible parties, and other relevant information to ensure that work is carried out according to established processes and rules. Workflows typically involve coordination and interaction among multiple participants and systems.

Workflows reduce system complexity by breaking down complex tasks into smaller subtasks (nodes), decreasing reliance on prompt engineering and model inference capabilities, enhancing the performance of LLM applications for complex tasks, and improving the system’s interpretability, stability, and fault tolerance.

Types of Nodes Used

In the implementation process, we will use the following types of nodes:

  1. Start Node: Configures the initial parameters for program startup.
  2. HTTP Request Node: Sends HTTP requests to fetch data.
  3. Iteration Node: Iterates over an array to execute multiple steps.
  4. Parameter Extractor Node: Processes and extracts parameters.
  5. Template Node: Allows flexible data transformation and text processing using the Jinja2 Python templating language.
  6. LLM Node: Calls a large language model to process natural language.
  7. Send Feishu Message Node: Pushes the curated news to Feishu.

Operational Steps

Step 1: Start

start.png

Through the start node, we can configure the initial parameters for program startup, such as API Key, operational categories, etc. This marks our first step. Next, we will begin fetching the desired data.

Step 2: HTTP Request

Taking Hacker News as an example, first find the API to get the list of data: https://hacker-news.firebaseio.com/v0/beststories.json?print=pretty.

Create an HTTP request node, configuring the HTTP request parameters including URL, request headers, query parameters, request body content, and authentication information.

http.png

The return value of the HTTP request includes the response body, status code, response headers, and files. These variables can be directly used in subsequent nodes, which is very convenient.

After configuring the node, we can try running it first.
http_result.png

The request was successful, and we received the article list IDs. Everything is going smoothly; next, we will iterate over the IDs to fetch the article details.

Step 3: Iteration

The purpose of iteration is to execute multiple steps on an array until all results are output, making it a powerful tool for repetitive tasks. Application scenarios include: long article generators, traversing requests, etc.

Simply input the IDs obtained from the article list into the iterator.
Iteration.png

However, after connecting to the iteration node, we found no available variables. Checking the documentation reveals:

The condition for using iteration is to ensure that the input value is formatted as a list object.

In the return format of the above list request, the body is a String. Therefore, we need to process the returned result and introduce a Parameter Extractor before the iteration node.
Paras.png

Set the list return body as input, and configure the parameter extraction to be a numeric array of IDs. A simple instruction declaration:

example: 
body: [1,2,3,4,5...500]
return Array[Number], and keep only 10

This not only allows us to extract the parameters but also preprocess them, such as limiting the number of results. In this way, we can ensure that when linking to the iteration node, we obtain a formatted and compliant array input parameter ids.
Para2.png

In the iteration node, we can access each element during each iteration, i.e., each article’s id. This way, we can further process each id, such as sending a new HTTP request to fetch the article’s detailed information. Thus, we can handle each article’s id one by one, ensuring that each article is correctly fetched and processed.

7.png

Next, we add the HTTP node for requesting details, and running it will yield the detailed results.

Learning from the previous experience, we note that the output variable of the iterator is Array[String].

Next, we will use the LLM node to organize and summarize the returned results. Let’s take a look at the LLM node.

Step 4: LLM Node

The LLM node’s purpose is to call a large language model to answer questions or process natural language.

If you are using Dify for the first time, before selecting a model in the LLM node, you need to complete the model configuration in System Settings - Model Provider.

We create a new LLM node linked to the iteration and configure our prompt. When we input User content, we find that we cannot retrieve the article details because the LLM node does not support array format input parameters, so we need to convert the array to text to proceed.

8.png

By checking the documentation on how to convert an array to text, we learn that we need to use either a Code Node or a Template Node for conversion.

Here we use the Template Node for conversion:
9.png

10.png

Now our LLM node can run normally; click run to debug.

11.png

Successfully executed.

Step 5: Send Feishu Message

Next, we will push the curated news to Feishu by adding a node - Tool - Send Feishu Message.

After creating a bot in the Feishu group, obtain the WEBHOOK KEY and fill it in:
12.png

Implementation Results

Run it again, and the results display:
13.png

Thus, we have completed a Dify application for news retrieval - data processing - pushing to IM. Through this process, we demonstrated how to use Dify’s workflows to achieve automated news pushing and experienced the powerful features and convenience of workflows.

Conclusion

Today, we learned how to use Dify’s workflows to build a news push application. Starting from configuring initial parameters, we gradually fetched data through HTTP requests, processed data using iteration nodes, organized data through LLM nodes, and finally pushed the curated news to Feishu. The entire process not only showcased the powerful capabilities of workflows but also allowed us to experience the convenience of automated processing.

Of course, the powerful features of Dify workflows go far beyond this. It also offers more nodes and functionalities waiting for us to explore and apply. We will continue to publish related articles to guide everyone in further learning and exploring more possibilities of Dify workflows.

Tags
Dify
workflow
feishu