private WeixinManager(WeixinConfig config) { Client = new WxClient(config.ApiUrl, new DefaultTokenContainer(config.ApiUrl, config.AppId, config.AppSecret)); Receiver = new WxReceiver(config.Token, config.EncodingAESKey); Receiver.ReceiveTextMessage += Receiver_ReceiveTextMessage; Receiver.SubscribeEvent += Receiver_SubscribeEvent; }
/// <summary> /// 获取微信授权信息 /// </summary> /// <param name="code">微信授权码</param> /// <returns></returns> internal static async Task <WxToken> GetTokenAsync(string code = null) { string url = $"{WxConstants.CGI_BIN_TOKEN_URL}?grant_type=client_credential&appid={WxClient.WxOption.AppId}&secret={WxClient.WxOption.AppSecret}"; if (!string.IsNullOrEmpty(code)) { url = $"{WxConstants.SNSAPI_OAUTH2_URL}?appid={WxClient.WxOption.AppId}&secret={WxClient.WxOption.AppSecret}&code={code}&grant_type=authorization_code"; return(await WxClient.GetAsync <WxToken>(url)); } if (!WxClient.MemoryCache.TryGetValue(WxConstants.ACCESS_TOKEN_KEY, out WxToken token)) { token = await WxClient.GetAsync <WxToken>(url); WxClient.MemoryCache.Set(WxConstants.ACCESS_TOKEN_KEY, token, TimeSpan.FromSeconds(token.ExpiresIn)); } return(token); }
/// <summary> /// 获取微信授权信息 /// </summary> /// <param name="client">httpclient</param> /// <param name="cache">memorycache</param> /// <param name="logger">日志记录器,记录返回信息</param> /// <param name="option">微信配置信息</param> /// <param name="code">微信授权码</param> /// <returns></returns> internal static async Task <string> GetTicketAsync() { var token = await AccessTokenFactory.GetTokenAsync(); if (token == null) { return(string.Empty); } string url = $"{WxConstants.JSAPI_TICKET_URL}?access_token={token.AccessToken}&type=jsapi"; if (!WxClient.MemoryCache.TryGetValue(WxConstants.JS_API_TICKET_KEY, out WxTicket ticket)) { ticket = await WxClient.GetAsync <WxTicket>(url); WxClient.MemoryCache.Set("wechat_jsapi_ticket", ticket.Ticket, TimeSpan.FromSeconds(ticket.ExpiresIn)); } return(ticket.Ticket); }
private void OnWxEvent(WxClient sender, WeChatClientEvent e) { RunInMainthread(() => { log(e.ToString()); if (e is GetQRCodeImageEvent) { var qrEvent = e as GetQRCodeImageEvent; pictureBox_qrcode.Image = qrEvent.QRImage; label_tips.Text = "用手机微信扫码登陆"; } if (e is UserScanQRCodeEvent) { var scanedQrEvent = e as UserScanQRCodeEvent; pictureBox_qrcode.Image = scanedQrEvent.UserAvatarImage; label_tips.Text = "在手机上确认以登陆"; } if (e is LoginSucessEvent) { var loginEvent = e as LoginSucessEvent; label_tips.Text = "登陆成功,正在同步消息..."; } if (e is InitedEvent) { var eve = e as InitedEvent; label_tips.Text = string.Format("[{0}]微信机器人正在运行中...", wx.Self.NickName); wx.SendMsg(wx.Self.ID, "图灵机器人启动了"); } if (e is AddMessageEvent) { var msgEvent = e as AddMessageEvent; if (msgEvent.Msg is TextMessage) { var msg = msgEvent.Msg as TextMessage; if (msg.Content == "老王") { if (!groups.Contains(msg.ToContactD)) { groups.Add(msg.ToContactD); wx.SendMsg(msg.ToContactD, "[老王]我在呢"); return; } } if (msg.Content == "老王 退下吧") { if (groups.Contains(msg.ToContactD)) { groups.Remove(msg.ToContactD); wx.SendMsg(msg.ToContactD, "[老王]好,我退下了"); return; } } if ((msg.ToContactD == wx.Self.ID || groups.Contains(msg.ToContactD)) && !msg.Content.StartsWith("[老王]")) { string uid = msg.FromContactID.Remove(0, 2); if (uid.Length > 32) { uid = uid.Substring(0, 32); } var info = msg.Content; string rep = tulingBot(info, uid); if (msg.ToContactD.StartsWith("@@")) { wx.SendMsg(msg.ToContactD, "[老王]" + rep); } else if (msg.ToContactD.StartsWith("@")) { wx.SendMsg(msg.FromContactID, "[老王]" + rep); } else { wx.SendMsg(msg.ToContactD, "[老王]" + rep); } log("[收到消息]" + info); log("[老王]" + rep); } } } }); }