Code instance sharing for WeChat developers to reply to User messages

Source: Internet
Author: User
Here is a summary of the knowledge to share the experience, hope to learn together ..... I think it's better to write a series later .... Enterprise Requirements: send messages to specified enterprise users, such as attendance information, enterprise notifications, and personal information push.

Here is a summary of the knowledge to share the experience, hope to learn together .....

I think it's better to write a series later ....

Enterprise requirements:

Send a message to a specified enterprise user at http://www.php.cn/php/php-s:for example, attendance information, enterprise notifications, and personal information push,

//////ID of the person to be sent///MessagePrivate void SendMessage (string UserID, string StrMessg) {Como. logUtil. writeLog ("Reply to user" + UserID + "message"); string Access_Token = Como. getAccessToken. getAccess_token (); if (Access_Token = ") Como. logUtil. writeException ("SendMessage failed to load Access_Token"); string Text = @ "{" "touser": "; Text + = '"' + UserID + '"'; text + = "," + '"' + @" msgtype ":" "text" "," "agentid" ":" 5 "", "" text "": {"" content ":"; Text + = '"' + StrMessg + '"'; Text + = @"}, "" safe "": "0" "}"; string url = String. format (" https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token= {0} ", Access_Token); string strResult = Como. tools. getPage (url, Text); JArray ja = (JArray) JsonConvert. deserializeObject ("[" + strResult + "]"); string Error = ""; try {if (strResult. contains ("errcode") {Error = ja [0] ["errcode"]. toString () ;}} catch (Exception ex) {Como. logUtil. writeException ("get strResult, error message not obtained" + ex. message. toString ();} string errcode = ja [0] ["errcode"]. toString (); strin G errmsg = ja [0] ["errmsg"]. toString (); if (errcode = "0" & errmsg = "OK") {Como. logUtil. writeLog ("reply successful! ");} Else {Como. LogUtil. WriteLog (" failed to reply! "); Como. LogUtil. WriteException (" failed to reply: SendMessage: "+ strResult );}}
////// Return the request result based on the request address and data ////////////
 Public static string GetPage (string posturl, string postData) {Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; encoding encoding = Encoding. UTF8; byte [] data = encoding. getBytes (postData); // prepare the request... try {// set the parameter request = WebRequest. create (posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer (); request. cookieContainer = cookieContainer; request. allowAutoRedirect = true; request. method = "POST"; request. contentType = "application/x-www-form-urlencoded"; request. contentLength = data. length; outstream = request. getRequestStream (); outstream. write (data, 0, data. length); outstream. close (); // send the request and obtain the response data response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request instream = response to the target webpage. getResponseStream (); sr = new StreamReader (instream, encoding); // return result webpage (html) code string content = sr. readToEnd (); string err = string. empty; return content;} catch (Exception ex) {string err = ex. message; Como. logUtil. writeException ("Tools GetPage exception:" + err. toString (); return string. empty ;}}

Parameter reference details:

1. Como. GetAccessToken. GetAccess_token () // get Access_token

2. GetPage // return the page request information result

The above is the basis for sending key code:

The following is an example of a successful instance:

The above is the details shared by the code instance for developing and replying to user messages. For more information, see other related articles on php Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.