public IActionResult PublicAccountCallback([FromQuery] string signature, [FromQuery] string timestamp, [FromQuery] string nonce, [FromQuery] string echostr) { try { _logger.Info(echostr, signature, timestamp, nonce); CheckSignature checkSignature = new CheckSignature(_weChatSettings.Value.token); if (checkSignature.IsValidSignature(timestamp, nonce, signature)) { return(Ok(echostr)); } else { return(Unauthorized()); } } catch (Exception e) { _logger.Error(e.ToString()); return(StatusCode(500)); } }
public async Task <IActionResult> PublicAccountCallbackPost([FromQuery] string signature, [FromQuery] string timestamp, [FromQuery] string nonce, [FromQuery] string echostr) { try { CheckSignature checkSignature = new CheckSignature(_weChatSettings.Value.token); // Check if signature matches if (!checkSignature.IsValidSignature(timestamp, nonce, signature)) { // Unauthorized call _logger.Warn("Unauthroized call!", Request.HttpContext.Connection.RemoteIpAddress?.ToString(), $"signature:{signature}, nonce:{nonce}, echostr:{echostr}"); return(Unauthorized()); } // Authorized call using (var reader = new StreamReader(Request.Body)) { // Read message var body = await reader.ReadToEndAsync(); // log original response _logger.Info(body); var messageXml = Formatting <MessageXml> .XmlToClass(body); // Check access_token if (!_cacheControl.IsCacheExist("access_token")) { // access token is not cached or expired, using wechat API to get a new one, then save it in cache Task <string> taskGetAccessToken = _weChatAPI.GetAccessTokenAsync(); taskGetAccessToken.Wait(); _accessToken = Formatting <AccessToken> .JsonToClass(taskGetAccessToken.Result).access_token; _cacheControl.SetCache("access_token", _accessToken, 60 * 60); } else { // access token is cached, retrieve it _accessToken = _cacheControl.GetValueBykey("access_token").ToString(); } // Different handlers for each type of message if (messageXml.MsgType == "text") { TextMessageXml textMessageXml = Formatting <TextMessageXml> .XmlToClass(body); _logger.Info($"User ({textMessageXml.FromUserName}) post text message {textMessageXml.Content}"); // fetch user information // IF username is already existed in somewhere like a local database, then we do not need to get info every time. // But it is a callback so performance wise it does not really matter so much. Task <string> taskGetUserInfo = _weChatAPI.GetSubscriberInfo(_accessToken, textMessageXml.FromUserName); taskGetUserInfo.Wait(); WeChatUserInfo weChatUserInfo = Formatting <WeChatUserInfo> .JsonToClass(taskGetUserInfo.Result); // TODO: auto reply } else if (messageXml.MsgType == "voice") { VoiceMessageXml voiceMessageXml = Formatting <VoiceMessageXml> .XmlToClass(body); _logger.Info($"User ({voiceMessageXml.FromUserName}) post voice message {voiceMessageXml.Recognition}. (Media Id:{voiceMessageXml.MediaId}, Format: {voiceMessageXml.Format})"); } else if (messageXml.MsgType == "image") { ImageMessageXml imageMessageXml = Formatting <ImageMessageXml> .XmlToClass(body); _logger.Info($"User ({imageMessageXml.FromUserName}) post image {imageMessageXml.PicUrl}. (Media Id:{imageMessageXml.MediaId})"); } } return(Ok(echostr)); } catch (Exception e) { _logger.Error("************"); _logger.Error(e.ToString()); _logger.Error("************"); return(Ok(echostr)); } }