Wechat Public Number Configuration and Use of Customer Service Messages

Keywords: C# encoding xml JSON

Brief introduction of customer service news

When user and public number interact with specific actions (see below for a list of specific actions), Wechat will push message data to the developer, who can call the customer service interface for a period of time (currently revised to 48 hours) and send messages to ordinary users through a JSON packet of POST. This interface is mainly used in customer service and other functions with manual message processing links to facilitate developers to provide users with more quality services.

At present, the list of permitted actions is as follows (Public Platform will update the list according to the operation situation. After triggering different actions, the number of messages sent under the allowed customer service interface is different. When the number of messages sent below reaches the upper limit, error return codes will be encountered. For details, see the Return Code Description Page):

1. User Sending Information
2. Click on the custom menu (only click on push event, sweep push event, sweep push event and pop up the "message receiving" prompt box, these three menu types will trigger the customer service interface)
3. Focus on the Public Number
4. Scanning two-dimensional code
5. Successful Payment
6. User Rights Defense
In order to help public numbers use different customer service identities to serve different user groups, the customer service interface has been upgraded. Developers can manage customer service accounts and set up their avatars and nicknames. This capability is open to all public numbers with access to customer service interfaces.

Log on to Wechat Public Platform and Open Customer Service Function

After opening the customer service function, when the user sends messages to the public number, Wechat will push the message data to the developer, where the message can be obtained in the uri address interface configured to receive.

After getting the message here, we need to forward some of the message to the Wechat customer service system.

Setting up message forwarding

Setting Message Forwarding Document Address
According to the document, set the corresponding message as follows

 string res = @"<xml>
                            <ToUserName><![CDATA[{0}]]></ToUserName>
                            <FromUserName><![CDATA[{1}]]></FromUserName>
                            <CreateTime>{2}</CreateTime>
                            <MsgType><![CDATA[transfer_customer_service]]></MsgType>
                            </xml>";
            return res;

Just replace the ToUserName FromUserName CreateTime inside, and be careful not to have spaces and newlines, otherwise the message forwarding will not succeed.

Add Customer Service Account

Document address

/// <summary>
        /// Add Customer Service Account 
        /// </summary>
        /// <param name="kf_account"></param>
        /// <param name="nickname"></param>
        /// <returns></returns>
        public string AddInviteworker(string kf_account, string nickname)
        {
            string token = GetWxToken();
            string url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" + token;
            using (WebClient webClient = new WebClient())
            {
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                string sResponse = webClient.UploadString(url, "POST", JsonConvert.SerializeObject(new
                {
                    kf_account,
                    nickname
                }));
                var res1 = JsonConvert.DeserializeObject<dynamic>(sResponse);
                LogHelper.Error(res1);
                return sResponse;
            }
        }

Invite Binding Micro Credit Users to Succeed Customer Service after Added Successfully

 /// <summary>
        /// Invite Binding Customer Service Account
        /// </summary>
        /// <param name="kf_account">complete customer service account in the form of: account prefix @public number microsign </param>
        /// <param name="invite_wx">customer service micro-signal </param>receiving binding invitation
        /// <returns></returns>
        public string BindInviteworker(string kf_account,string invite_wx) {
            string token = GetWxToken();
            string url = "https://api.weixin.qq.com/customservice/kfaccount/inviteworker?access_token=" + token;
            using (WebClient webClient = new WebClient())
            {
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                string sResponse = webClient.UploadString(url, "POST", JsonConvert.SerializeObject(new {
                    kf_account,
                    invite_wx
                })) ;
                var res1 = JsonConvert.DeserializeObject<dynamic>(sResponse);
                Console.WriteLine(sResponse);
                return sResponse;
            }
        }

Send messages, you can send messages through interface calls, or you can log in to the Wechat Public Platform Customer Service Web page to send messages directly.

   /// <summary>
        /// Send customer messages
        /// </summary>
        /// <param name="touser"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public  string SendMsgWechat(string touser,string content) {

            string token = GetWxToken();
            string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+ token;
            using (WebClient webClient = new WebClient())
            {
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                string sResponse = webClient.UploadString(url, "POST", JsonConvert.SerializeObject(new {
                    touser,
                    msgtype= "text",
                    text = new {
                        content= content
                    }
                }));
                var res1 = JsonConvert.DeserializeObject<dynamic>(sResponse);
                LogHelper.Error(res1);
                return sResponse;
            }

        }

Send messages directly in Wechat Customer Service Web page

Posted by volka on Mon, 26 Aug 2019 01:53:12 -0700