Implementing WeChat Template Notification Services with Magicodes.WeiChat

WeChat Platfomr Configuration

Integrating transactional notifications often requires utilizing WeChat's template message capability. Within the framework ecosystem, this functionality is abstracted to simplify backend management and logging. To begin, administrators must enable the tepmlate service within the Official Account dashboard.

Initiate the process by locating the template management section. Submit an application for the desired template type and await platform verification. Once approved, browse the official template library to select templates appropriate for your business scenarios, such as order confirmations or service updates.

After selecting a template, retrieve its unique identifier, which serves as the reference key for subsequent API calls.

Admin Console Operations

The framework provides an administrative interface to manage these configurations externally. Navigate to the designated message module within the back-office system.

This console allows you to preview how the message will appear before dispatch. Users can interact with editable fields—typically denoted by specific formatting—and enter the necessary parameter values required by the template structure. Upon finalizing the content, execute the send commend to deliver the notification to the test group or selected recipients.

Programmatic Integration

For automated workflows, the Magicodes.WeiChat framework exposes a dedicated client interface located within the Magicodes.WeiChat.Framework assembly. The core entity responsible for constructing payloads is designed to align with the official API specifications.

The primary object defines the structure of the outgoing request, mapping OpenIDs, template identifiers, top banner colors, and dynamic data fields.

public class SendTemplateMessageOptions 
{
    /// <summary>
    /// Target recipient OpenIDs. Multiple IDs should be semicolon-separated.
    /// </summary>
    public string RecipientIds { get; set; }

    /// <summary>
    /// Unique identifier for the chosen template.
    /// </summary>
    public string TemplateIdentifier { get; set; }

    /// <summary>
    /// Hexadecimal code for the header bar color (optional).
    /// </summary>
    public string HeaderColor { get; set; }

    /// <summary>
    /// Destination URL triggered upon message click.
    /// </summary>
    public string RedirectUrl { get; set; }

    /// <summary>
    /// Dictionary mapping template field names to their content values and styling.
    /// </summary>
    public IDictionary<string, FieldItem> PayloadData { get; set; }
}

Developers can instantiate the payload builder to process incoming requests dynamically. The following example demonstrates how to extract form data associated with specific template variables and invoke the messaging API.

// Construct list of target recipients from current session data
var userList = GetActiveUserRecords();
string openIdBatch = string.Join(";", userList.Select(u => u.OpenId));

// Prepare the notification payload
var messagePayload = new SendTemplateMessageOptions 
{
    RecipientIds = openIdBatch.TrimEnd(';'),
    TemplateIdentifier = "required_template_id",
    RedirectUrl = "https://example.com/result"
};

// Map form inputs to template fields programmatically
foreach (var key in HttpContext.Request.Form.Keys)
{
    if (key.EndsWith("_DATA"))
    {
        string fieldName = key.Substring(0, key.Length - 5); // Remove suffix
        string value = HttpContext.Request.Form[key];
        
        // Optional: Retrieve custom color settings if provided
        string color = HttpContext.Request.Form[$"{key}_COLOR"];
        
        messagePayload.PayloadData.Add(fieldName, new FieldItem(value, color));
    }
}

// Execute the API call via the registered context
var apiContext = WeiChatApisContext.GetCurrent();
var apiService = apiContext.MessageSender;
var result = await apiService.SendAsync(messagePayload);

return result;

Ensure that all interaction with the template service utilizes the centralized API context provider to guarantee proper token management and error handling.

Tags: wechat-api Magicodes.WeiChat dotnet-core Template-Messages csharp

Posted on Mon, 05 Oct 2026 16:49:55 +0000 by phyzar