# customerServiceMessage.send

Call this API at the server side. For more information, see Server API.

This API supports Cloud Calls. The WeChat DevTools version must be 1.02.1904090 or later (download the latest stable version here), and the wx-server-sdk version must be 0.4.0 or later.

Sends a customer message to the user. For detailed rules, see Sending a Customer Service Message.

Calling methods:

# HTTPS Call

# Request Address

POST https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN

# Request Parameters

Attribute Type Default Required Description
access_token string Yes Credentials to call API
touser string Yes The user's OpenID.
msgtype string Yes Message type.
content string Yes The content of the text message. It is required when msgtype="text".
image Object Yes Image message. It is required when msgtype="image".
link Object Yes Message with an image link. It is required when msgtype="link".
miniprogrampage Object Yes Mini Program card. It is required when msgtype="miniprogrampage".

Valid values of msgtype

Value Description Minimum Version
text Text message
image Image message
link Message with image/text links
miniprogrampage Mini Program card

image is composed as follows

Property Type Default Required Description
media_id string Yes The media ID of the sent image. It is returned when uploading image files via the media uploading API.

link is composed as follows

Property Type Default Required Description
title string Yes Message title
description string Yes Message with image/text links
url string Yes URL to which a message with image/text links is redirected
thumb_url string Yes The image link in a message with image/text links. The JPG and PNG formats are supported. Preferred resolutions are 640x320 pixels for large pictures and 80x80 pixels for small pictures.

miniprogrampage is composed as follows

Property Type Default Required Description
title string Yes Message title
pagepath string Yes The page path of a Mini Program. It is aligned with app.json, and supports parameters, for example, pages/index/index?foo=bar.
thumb_media_id string Yes The cover of a Mini Program message card. It is the media_id of an image, and is returned when uploading an image file via the media uploading API. The recommended resolution is 520x416 pixels.

# Return Value

# Object

JSON data package that is returned

Attribute Type Description
errcode number Error code
errmsg string Error message

Valid values of errcode

Value Description Minimum Version
0 Request successful
-1 System is busy. Try again later.
40001 Incorrect AppSecret or invalid access_token. Check the accuracy of AppSecret or check whether the API is called for a proper Mini Program.
40002 Invalid credential type
40003 Invalid OpenID. Check whether the OpenID belongs to another Mini Program.
45015 Response timed out
45047 The number of downstream customer service messages exceeds the upper limit.
48001 Unable to access the API. Check whether the Mini Program has access to the API.

# Message Sending Example

# Sending a text message

{
  "touser":"OPENID",
  "msgtype":"text",
  "text":
  {
    "content":"Hello World"
  }
}

When sending a text message, you can add a text link that can redirect to a Mini Program.

Text content...<a href="http://www.qq.com" data-miniprogram-appid="appid" data-miniprogram-path="pages/index/index">Tap to redirect to the Mini Program</a>
# Description:
  1. In data-miniprogram-appid, enter the AppID of the Mini Program. By tapping the link, the user is redirected to the Mini Program.
  2. In data-miniprogram-path, enter the path of the Mini Program. The path must be the same as that in app.json and can contain parameters.
  3. For WeChat 6.5.16 and earlier that do not support data-miniprogram-appid, if a herf is available, the link in the herf is still used to redirect to the Mini Program.
  4. When a Mini Program sends a text message that contains a Mini Program text link, the data-miniprogram-appid must be the AppID of the Mini Program.

# Sending an image message

{
  "touser":"OPENID",
  "msgtype":"image",
  "image": {
    "media_id":"MEDIA_ID"
  }
}

You can send one message with image/text links at a time.

{
  "touser": "OPENID",
  "msgtype": "link",
  "link": {
    "title": "Happy Day",
    "description": "Is Really A Happy Day",
    "url": "URL",
    "thumb_url": "THUMB_URL"
  }
}

# Sending a Mini Program Card

{
 "touser":"OPENID",
 "msgtype":"miniprogrampage",
 "miniprogrampage": {
   "title":"title",
   "pagepath":"pagepath",
   "thumb_media_id":"thumb_media_id"
 }
}

# Cloud Call

Cloud call is a capability provided by Mini Program·Cloud Base that allows you to call WeChat APIs in a cloud function. It must be used via wx-server-sdk in the cloud function.

# API Calling Method

openapi.customerServiceMessage.send

You need to configure the permissions for the customerServiceMessage.send API via config.json. Details

# Request Parameters

Attribute Type Default Required Description
touser string Yes The user's OpenID.
msgtype string Yes Message type.
content string Yes The content of the text message. It is required when msgtype="text".
image Object Yes Image message. It is required when msgtype="image".
link Object Yes Message with an image link. It is required when msgtype="link".
miniprogrampage Object Yes Mini Program card. It is required when msgtype="miniprogrampage".

Valid values of msgtype

Value Description Minimum Version
text Text message
image Image message
link Message with image/text links
miniprogrampage Mini Program card

image is composed as follows

Property Type Default Required Description
mediaId string Yes The media ID of the sent image. It is returned when uploading image files via themedia uploading API.

link is composed as follows

Property Type Default Required Description
title string Yes Message title
description string Yes Message with image/text links
url string Yes URL to which a message with image/text links is redirected
thumbUrl string Yes The image link in a message with image/text links. The JPG and PNG formats are supported. Preferred resolutions are 640x320 pixels for large pictures and 80x80 pixels for small pictures.

miniprogrampage is composed as follows

Property Type Default Required Description
title string Yes Message title
pagepath string Yes The page path of a Mini Program. It is aligned with app.json, and supports parameters, for example, pages/index/index?foo=bar.
thumbMediaId string Yes The cover of a Mini Program message card. It is the media_id of an image, and is returned when uploading an image file via the media uploading API. The recommended resolution is 520x416 pixels.

# Return Value

# Object

JSON data package that is returned

Attribute Type Description
errCode number Error code
errMsg string Error message

Valid values of errCode

Value Description Minimum Version
0 Succeeded

# Exceptions

# Object

Thrown Exceptions

Property Type Description
errCode number Error code
errMsg string Error message

Valid values of errCode

Value Description Minimum Version
-1 System is busy. Try again later.
40001 Incorrect AppSecret or invalid access_token. Check the accuracy of AppSecret or check whether the API is called for a proper Mini Program.
40002 Invalid credential type
40003 Invalid OpenID. Check whether the OpenID belongs to another Mini Program.
45015 Response timed out
45047 The number of downstream customer service messages exceeds the upper limit.
48001 Unable to access the API. Check whether the Mini Program has access to the API.

# Message Sending Example

# Sending a text message

const cloud = require('wx-server-sdk')
  cloud.init()
  exports.main = async (event, context) => {
    try {
      const result = await cloud.openapi.customerServiceMessage.send({
        touser: 'OPENID',
        msgtype: 'text',
        text: {
          content: 'Hello World'
        }
      })
      console.log(result)
      return result
    } catch (err) {
      console.log(err)
      return err
    }
  }

When sending a text message, you can add a text link that can redirect to a Mini Program.

Text content...<a href="http://www.qq.com" data-miniprogram-appid="appid" data-miniprogram-path="pages/index/index">Tap to redirect to the Mini Program</a>
# Description:
  1. In data-miniprogram-appid, enter the AppID of the Mini Program. By tapping the link, the user is redirected to the Mini Program.
  2. In data-miniprogram-path, enter the path of the Mini Program. The path must be the same as that in app.json and can contain parameters.
  3. For WeChat 6.5.16 and earlier that do not support data-miniprogram-appid, if a herf is available, the link in the herf is still used to redirect to the Mini Program.
  4. When a Mini Program sends a text message that contains a Mini Program text link, the data-miniprogram-appid must be the AppID of the Mini Program.

# Sending an image message

const cloud = require('wx-server-sdk')
  cloud.init()
  exports.main = async (event, context) => {
    try {
      const result = await cloud.openapi.customerServiceMessage.send({
        touser: 'OPENID',
        msgtype: 'image',
        image: {
          mediaId: 'MEDIA_ID'
        }
      })
      console.log(result)
      return result
    } catch (err) {
      console.log(err)
      return err
    }
  }

You can send one message with image/text links at a time.

const cloud = require('wx-server-sdk')
  cloud.init()
  exports.main = async (event, context) => {
    try {
      const result = await cloud.openapi.customerServiceMessage.send({
        touser: 'OPENID',
        msgtype: 'link',
        link: {
          title: 'Happy Day',
          description: 'Is Really A Happy Day',
          url: 'URL',
          thumbUrl: 'THUMB_URL'
        }
      })
      console.log(result)
      return result
    } catch (err) {
      console.log(err)
      return err
    }
  }

# Sending a Mini Program Card

const cloud = require('wx-server-sdk')
  cloud.init()
  exports.main = async (event, context) => {
    try {
      const result = await cloud.openapi.customerServiceMessage.send({
        touser: 'OPENID',
        msgtype: 'miniprogrampage',
        miniprogrampage: {
          title: 'title',
          pagepath: 'pagepath',
          thumbMediaId: 'thumb_media_id'
        }
      })
      console.log(result)
      return result
    } catch (err) {
      console.log(err)
      return err
    }
  }