---
title: Building AI Bots with RongCloud Instant Messaging and Dify
---

> Author: wangqijia｜RongCloud Developer Team
> Date: June 2025

## 1. Overview
[RongCloud IM PaaS](https://www.rongcloud.cn/) achieves seamless deep integration with the Dify platform, establishing an end-to-end conversational channel between end users and large language models (LLMs). It supports streaming responses and multi-turn context, widely applied in scenarios such as intelligent Q&A, content creation, and emotional companionship, helping enterprises build efficient and intelligent user interaction experiences.

The platform provides comprehensive [AI bot](https://docs.rongcloud.cn/platform-chat-api/bot/overview) management capabilities, supporting richer event callbacks triggered by specified bots, assisting developers in flexibly handling various business scenarios, such as:

* Initiating one-on-one conversations with bots
* Triggering contextual responses by mentioning (@) bots

Additionally, the platform has built-in AI platform integration capabilities, allowing bot messages to be automatically forwarded to connected large model platforms (such as Dify), eliminating the need for intermediate processing services, effectively improving link stability, reducing integration costs, and accelerating development and delivery.

This guide will explain how to integrate AI bots based on the RongCloud IM platform and connect to large model services via the Dify platform.

---

## 2. High-Level Architecture

<img src="https://docs.rongcloud.cn/platform-chat-api/assets/bot/bot-dify-high-level-design.jpg" alt="Diagram" width="80%" />

**Workflow:**

1. **App Server**

  * Manages bots, processes webhook callbacks, and receives bot messages.

2. **User Client**

  * Sends user messages and receives bot responses.

3. **IM Server**

  * **Message Forwarding**: Forwards messages to Dify based on callback types (e.g., `dify_chat`, `dify_completion`) and supports both streaming and non-streaming modes.
  * **Response Handling and Session Management**:

  * *Streaming*: Pushes incremental content from Dify in real time.
  * *Non-streaming*: Sends the complete message after receiving the full response.
  * *Session Context*: Stores `conversation_id` from the first interaction (valid for 24 hours). Subsequent messages use this ID to maintain multi-turn context.

4. **Dify**

* Receives the message, performs inference, and returns either streaming or complete text responses.

---

## 3. Integration and Deployment Guide

This section helps you deploy and configure the integration quickly, enabling you to experience intelligent bot interactions on RongCloud IM.

### 3.1 Prerequisites

Before integrating your bot, please ensure the following:

* You have created an application on the **RongCloud Developer Console** and obtained a valid `App Key` and `App Secret`.
* Dify is deployed (either self-hosted or via a cloud service).

### 3.2 Supported Callback Types

Callback types determine how bots interact with different platforms or systems. Currently supported types include:

| Type                 | Description                                                             |
| -------------------- | ----------------------------------------------------------------------- |
| **webhook**          | Custom callback via webhook, suitable for flexible integration          |
| **dify\_chat**       | Connects to Dify’s “Chat Assistant” mode for building multi-turn bots   |
| **dify\_completion** | Connects to Dify’s “Text Completion” mode for generative applications   |
| **dify\_chatflow**   | Connects to Dify’s “Chatflow” mode for flow-based conversational design |

### 3.3 Integration Steps

1. **Create a Dify App**: Create an app in Dify and obtain the API key.
2. **Create a Real User**: Create a real user account to interact with the bot.
3. **Configure the Bot and Callback Address**: Register your bot and set up the message callback.
4. **Verify Integration**: Send a private message from the real user to the bot and confirm the response from Dify.
5. **Verify Multi-Turn Conversation**: Continue sending messages from the same user to the bot and check if context is preserved.

⚠️ Note: RongCloud API calls require authentication. Use the provided script to generate credentials.

---

### Step 1: Create a Dify App

Message assistants connect using the `dify_chatflow` mode, which supports memory-based multi-turn workflows.

<img src="https://docs.rongcloud.cn/platform-chat-api/assets/bot/create-dify-app.png" alt="Diagram" width="80%" />

You can check the full DSL config example: [dify-chatflow-dsl.yml](https://docs.rongcloud.cn/platform-chat-api/assets/bot/dify-chatflow-dsl.yml)

---

### Step 2: Create a Real User

```bash
curl -X POST "https://api.rong-api.com/v3/bot/create.json" \
  -H "App-Key: YOUR_APP_KEY" \
  -H "Nonce: YOUR_NONCE" \
  -H "Timestamp: YOUR_TIMESTAMP" \
  -H "Signature: YOUR_SIGNATURE" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "userId=LoDld8izA"
```

---

### Step 3: Configure Bot and Callback Address

Depending on your scenario, choose between:

* **Streaming mode**: Best for long or incremental responses, offering a smoother user experience.
* **Non-streaming mode**: Best when responses are shown only after completion.

```bash
curl -X POST "https://api.rong-api.com/v3/bot/create.json" \
  -H "App-Key: YOUR_APP_KEY" \
  -H "Nonce: YOUR_NONCE" \
  -H "Timestamp: YOUR_TIMESTAMP" \
  -H "Signature: YOUR_SIGNATURE" \
  -H "Content-Type: application/json" \
  -d '{
    "userId": "bot-test004",
    "name": "Message Assistant",
    "type": "Message AI",
    "profileUrl": "https://cdn.jsdelivr.net/gh/microsoft/fluentui-emoji/assets/Robot/3D/robot_3d.png",
    "integrations": [
        {
            "type": "webhook",
            "callbackUrl": "https://yourserver.com/callback",
            "objectNames":["RC:TxtMsg"],
            "events":["message:private"]
        },
        {
            "type": "dify_chatflow",
            "callbackUrl": "https://api.dify.ai/v1",
            "stream": true,
            "auth": {
                "apiKey": "YOUR_DIFY_API_KEY"
            }
        }
    ],
    "metadata": {
        "version": 1
    }
}'
```

---

### Step 4: Verify Integration

Send a message from the real user created in step 2:

```bash
curl -X POST "https://api.rong-api.com/message/private/publish.json" \
  -H "App-Key: YOUR_APP_KEY" \
  -H "Nonce: YOUR_NONCE" \
  -H "Timestamp: YOUR_TIMESTAMP" \
  -H "Signature: YOUR_SIGNATURE" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "fromUserId=BesqX1FTg33" \
  --data-urlencode "toUserId=bot-001" \
  --data-urlencode "objectName=RC:TxtMsg" \
  --data-urlencode 'content={"content":"Can you give me a few AI use case ideas?","extra":""}'
```

> Demo screenshots from Sealtalk APP:

<div style={{ display: 'flex', justifyContent: 'space-between' }}>
  <img src="https://docs.rongcloud.cn/platform-chat-api/assets/bot/send-bot-message.png" alt="图示1" style={{ width: '70%' }} />
  <img src="https://docs.rongcloud.cn/platform-chat-api/assets/bot/send-bot-message2.png" alt="图示2" style={{ width: '70%' }} />
</div>

---

### Step 5: Verify Multi-Turn Conversations

Send multiple messages from the same user to the same bot and check if the context is preserved.

<img src="https://docs.rongcloud.cn/platform-chat-api/assets/bot/send-bot-message3.png" alt="Demo" width="50%" />

> The Dify log shows 4 messages, confirming that multi-turn conversation context is successfully maintained within the same session.

<img src="https://docs.rongcloud.cn/platform-chat-api/assets/bot/dify-log.png" alt="Log" width="50%" />

---

## 4. Use Cases

### 4.1 Message Assistant: Enable smart Q\&A, task execution, content generation

* Knowledge Q\&A powered by knowledge base or LLM
* Content generation requests (e.g., "Write an apology email")
* Translation requests (e.g., "Translate this to English")
* Instruction requests that trigger system actions (e.g., "Create a ticket")
* FAQ automation based on user intent parsing

### 4.2 Companion Bot: Create personalized, emotionally intelligent chatbots

* Natural casual conversations to fulfill daily social interaction needs
* Role simulation (e.g., friend, therapist, virtual idol) to enhance engagement
* Emotion-aware responses and empathetic interaction
* Combine with custom character settings (e.g., "AI Partner", "Career Coach") for immersive experiences

---

## 5. Best Practices

### 5.1 Use Metadata to Personalize Bot Context

When creating bots, you can pass custom metadata for the creator. This metadata will be included in message callbacks and forwarded to Dify. It can be used for:

* Prompt variable substitution
* Contextual customization
* Flow orchestration

### 5.2 Configure Separate Bots and Dify Apps per Scenario

It is recommended to create separate bots and Dify apps for each business use case (e.g., Q\&A, translation, tools, companionship). Assign specific prompts to each bot for better targeting and improved user experience.

---

## 6. Utility Scripts

### 6.1 Generate RongCloud HTTP Signature

```python
import hashlib
import time
import random


def generate_rongcloud_signature(app_secret, nonce=None, timestamp=None):
    """
    Generate RongCloud HTTP request signature.

    Parameters:
        app_secret: Your RongCloud App Secret
        nonce: Optional random string
        timestamp: Optional timestamp

    Returns:
        Dictionary with nonce, timestamp, and signature
    """
    if nonce is None:
        nonce = ''.join(random.choices('0123456789abcdefghijklmnopqrstuvwxyz', k=6))

    if timestamp is None:
        timestamp = str(int(time.time()))

    signature_str = app_secret + nonce + timestamp
    signature = hashlib.sha1(signature_str.encode('utf-8')).hexdigest()

    return {
        'nonce': nonce,
        'timestamp': timestamp,
        'signature': signature
    }


if __name__ == '__main__':
    app_secret = 'YOUR_APP_SECRET'
    signature_data = generate_rongcloud_signature(app_secret)

    print("Generated RongCloud Signature:")
    print(f"Nonce: {signature_data['nonce']}")
    print(f"Timestamp: {signature_data['timestamp']}")
    print(f"Signature: {signature_data['signature']}")
```

---

## Conclusion

Thank you for exploring this guide on integrating RongCloud IM PaaS with Dify LLM services. This powerful combination simplifies the AI bot development process by reducing technical complexity, while providing a robust callback mechanism and native multi-turn support.

Whether you're building a knowledge bot, a content generation tool, or a personalized companion chatbot, RongCloud and Dify together offer a stable and efficient solution.

As AI technology continues to evolve, we will keep enhancing platform compatibility and provide more tools and documentation. We welcome your feedback to help shape the future of intelligent dialogue systems.

**Wishing you success on your development journey and in your projects!**

{/*
Contributing Section
DO NOT edit this section!
It will be automatically generated by the script.
*/}

---

[Edit this page](https://github.com/langgenius/dify-docs/edit/main/en/learn-more/use-cases/ai-bot-rongcloud-dify-integration-guide.mdx) | [Report an issue](https://github.com/langgenius/dify-docs/issues/new?template=docs.yml)

