private void Check50Protocol(string line) { if (!string.IsNullOrEmpty(line)) { if (line.Contains(IQFeedConfiguration.Current50Protocol)) { _logger.Info("Current 5.0 protocol"); } else { _logger.Warn("Current not 5.0 protocol. Install IQFeed 5.0."); } } }
/// <summary> /// Delete book from list /// </summary> /// <param name="oldBook">Book to delete</param> public void RemoveBook(Book oldBook) { if (oldBook == null) { throw new ArgumentNullException(nameof(oldBook)); } logger.Debug("Removing book from list"); if (!bookList.Contains(oldBook)) { logger.Info("The book for deletion not found"); throw new Exception("Nothing to delete"); } if (!bookList.Remove(oldBook)) { logger.Warn("Can't delete book from bookList"); throw new Exception("Can't delete book"); } }
private List <Nominee> NomineesFromSunlightNominees(SunlightVoteResult.SunlightNominee[] sunlightNominees) { var nominees = new List <Nominee>(); if (sunlightNominees == null) { _logger.Warn("Cannot retrive nominees from SunlightNominees because the value is null!"); return(nominees); } foreach (var n in sunlightNominees) { var nominee = new Nominee(n.name ?? string.Empty, n.position ?? string.Empty, n.state); nominees.Add(nominee); } return(nominees); }
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)); } }