Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program message customer service message


May 18, 2021 WeChat Mini Program Development Document


Table of contents


Customer service messages

Use customer service messages on the page

The value of the button component open-type needs to be set to contact, and when the user clicks, it enters the customer service session, and if the user clicks on the small program message in the session, it returns to the small program, where the developer can get the page path path and corresponding parameter query of the message the user clicks through the bindcontact event callback.

Code example

<button open-type="contact" bindcontact="handleContact"></button>
Page({
    handleContact (e) {
        console.log(e.detail.path)
        console.log(e.detail.query)
    }
})

Returns a description of the parameters

Parameters Type Description
path String The path specified by the small program message
query Object The query parameters specified by the small program message

Background access messaging service

When a user sends a message to a small program customer service, or enters a session, the server configuration URL filled in by the developer (or, if using a cloud development, a configured cloud function) will receive messages and events pushed by the WeChat server, and the developer can respond according to his or her business logic. Please refer to Message Push for access and usage.


Receive messages and events

On the page, you can display the go-to-customer service session button by using the button open-type-"contact" />

When a user sends a message in a customer service session, or when an event push is raised by certain user actions, the WeChat server sends packets of the message or event to the URL filled out by the developer, and if the cloud development is used, it can be pushed to the specified cloud function (see Message Push for details). When a developer receives a request, they can use the Send customer service message interface to make an asynchronous reply.

The push JSON, XML data packet structure of each message type is as follows.

Text message

The user will generate the following packets when sending a text message in the customer service session:

XML format

<xml>
   <ToUserName><![CDATA[toUser]]></ToUserName>
   <FromUserName><![CDATA[fromUser]]></FromUserName>
   <CreateTime>1482048670</CreateTime>
   <MsgType><![CDATA[text]]></MsgType>
   <Content><![CDATA[this is a test]]></Content>
   <MsgId>1234567890123456</MsgId>
</xml>

JSON format

{
  "ToUserName": "toUser",
  "FromUserName": "fromUser",
  "CreateTime": 1482048670,
  "MsgType": "text",
  "Content": "this is a test",
  "MsgId": 1234567890123456
}

Description of the parameters

Parameters Description
ToUserName The original ID of the applet
FromUserName The sender's openid
CreateTime Message creation time (integer)
MsgType text
Content The content of the text message
MsgId Message id, 64-bit integer

Picture message

When a user sends a picture message in a customer service session, the following packets are generated:

XML format

<xml>
      <ToUserName><![CDATA[toUser]]></ToUserName>
      <FromUserName><![CDATA[fromUser]]></FromUserName>
      <CreateTime>1482048670</CreateTime>
      <MsgType><![CDATA[image]]></MsgType>
      <PicUrl><![CDATA[this is a url]]></PicUrl>
      <MediaId><![CDATA[media_id]]></MediaId>
      <MsgId>1234567890123456</MsgId>
</xml>

JSON format

{
  "ToUserName": "toUser",
  "FromUserName": "fromUser",
  "CreateTime": 1482048670,
  "MsgType": "image",
  "PicUrl": "this is a url",
  "MediaId": "media_id",
  "MsgId": 1234567890123456
}

Description of the parameters

Parameters Description
ToUserName The original ID of the applet
FromUserName The sender's openid
CreateTime Message creation time (integer)
MsgType image
PicUrl Photo link (generated by the system)
MediaId Picture message media id, you can call the getTempMedia interface to pull the data.
MsgId Message id, 64-bit integer

Small program card message

When a user sends a small program card message in a customer service session, the following packets are generated:

XML format

<xml>
  <ToUserName><![CDATA[toUser]]></ToUserName>
  <FromUserName><![CDATA[fromUser]]></FromUserName>
  <CreateTime>1482048670</CreateTime>
  <MsgType><![CDATA[miniprogrampage]]></MsgType>
  <MsgId>1234567890123456</MsgId>
  <Title><![CDATA[Title]]></Title>
  <AppId><![CDATA[AppId]]></AppId>
  <PagePath><![CDATA[PagePath]]></PagePath>
  <ThumbUrl><![CDATA[ThumbUrl]]></ThumbUrl>
  <ThumbMediaId><![CDATA[ThumbMediaId]]></ThumbMediaId>
</xml>

JSON format

{
  "ToUserName": "toUser",
  "FromUserName": "fromUser",
  "CreateTime": 1482048670,
  "MsgType": "miniprogrampage",
  "MsgId": 1234567890123456,
  "Title":"title",
  "AppId":"appid",
  "PagePath":"path",
  "ThumbUrl":"",
  "ThumbMediaId":""
}

Description of the parameters

Parameters Description
ToUserName The original ID of the applet
FromUserName The sender's openid
CreateTime Message creation time (integer)
MsgType miniprogrampage
MsgId Message id, 64-bit integer
Title Title
AppId Small program appid
PagePath The path to the small program page
ThumbUrl Temporary cdn link to cover picture
ThumbMediaId Temporary footage id for the cover picture

Enter the session event

The user will generate the following packets when the small program "Customer Service Session Button" enters the customer service session:

XML format

<xml>
    <ToUserName><![CDATA[toUser]]></ToUserName>
    <FromUserName><![CDATA[fromUser]]></FromUserName>
    <CreateTime>1482048670</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[user_enter_tempsession]]></Event>
    <SessionFrom><![CDATA[sessionFrom]]></SessionFrom>
</xml>

JSON format

{
  "ToUserName": "toUser",
  "FromUserName": "fromUser",
  "CreateTime": 1482048670,
  "MsgType": "event",
  "Event": "user_enter_tempsession",
  "SessionFrom": "sessionFrom"
}

Description of the parameters

Parameters Description
ToUserName The original ID of the applet
FromUserName The sender's openid
CreateTime Event creation time (integer)
MsgType event
Event The type of event, user_enter_tempsession
SessionFrom The session-from property set by the developer at the customer service session button

Send a customer service message

When the user and the small program customer service produce a specific action interaction (see the description below), WeChat will push the message data to the developer, the developer can call the customer service interface for a period of time (currently 48 hours), by calling the send customer service message interface to send messages to ordinary users. This interface is mainly used for customer service and other human message processing links, convenient for developers to provide users with more quality services.

The list of actions currently allowed is as follows, and after different actions are triggered, the number of message bars and time limits issued by the allowed customer service interface are different.

User action Allow a limit on the number of bars that can be issued Time limit for issuance
The user sends a message 5 48 hours

Forward customer service messages

If the small program set up a message push, ordinary WeChat users to the small program customer service message, WeChat server will first post the message POST to the developer filled in the URL, if you want to forward the message to the web version of the customer service tool, you need the developer in the response package to return MsgType transfer_customer_service message, WeChat server received a response will be sent the next message forwarded to the customer service system.

After the user is connected by customer service, before the customer service closes the session, when the session is in the process, the messages sent by the user are forwarded directly to the customer service system. When customer service is not turned off for more than 30 minutes, the WeChat server automatically stops forwarding to customer service and sends the message back to the URL filled out by the developer.

While the user waits in the queue, messages sent by the user are still pushed to the URL filled out by the developer.

It is especially important to note that only for WeChat users sent messages to forward, and for other events (such as users from small programs to arouse customer service sessions) should not be forwarded, otherwise customer service in the customer service system will see some meaningless messages.

The message is forwarded to the web version of the customer service tool

As soon as the developer returns a message with MsgType as transfer_customer_service in the response package, the WeChat server will forward the message to the customer service system when it receives the response.

If you are using a message push received by your own server, you will need to return XML data in the following format:

<xml>
    <ToUserName><![CDATA[touser]]></ToUserName>
    <FromUserName><![CDATA[fromuser]]></FromUserName>
    <CreateTime>1399197672</CreateTime>
    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>

Description of the parameters

Parameters Whether it is necessary Describe
ToUserName Is Recipient account number (OpenID received)
FromUserName Is Developer microsyscopy
CreateTime Is Message creation time (integer)
MsgType Is transfer_customer_service

If you are using a message push received by a cloud function, you need to return JSON data in the same format after the cloud function is triggered by a customer service message:

// ...
exports.main = async (event, context) => {
  // 判断处理客服消息 ...
  // 最后返回 JSON
  return {
    MsgType: 'transfer_customer_service',
    ToUserName: 'touser',
    FromUserName: 'fromuser',
    CreateTime: parseInt(+new Date / 1000),
  }
}

Customer service input status

Developers can return the current input status of customer service to the user by calling the customer service input status interface.

  1. This interface requires customer service message interface permissions.
  2. If the trigger condition for sending a customer service message is not met, the input status cannot be issued.
  3. Under-input status, you need customer service to have a message interaction with the user within 30 seconds.
  4. In the input state (lasts 15 seconds), the input state cannot be repeated.
  5. In the input state, if a message is sent to the user, the input state is also canceled.

Use temporary footage in customer service messages

Developers can obtain or upload temporary footage while receiving and sending customer service messages.

Get temporary footage

After you receive a user message, you can get temporary footage in the message through the Get temporary footage interface

Upload temporary footage

The Upload Temporary Footage interface allows you to upload temporary footage and use it in the Send Message interface.