public virtual async Task SendAsync(SubscribeMessage message, CancellationToken cancellationToken = default) { var weChatToken = await WeChatTokenProvider.GetTokenAsync(MiniProgramOptions.AppId, MiniProgramOptions.AppSecret, cancellationToken); var requestParamters = new Dictionary <string, string> { { "access_token", weChatToken.AccessToken } }; var weChatSendNotificationUrl = "https://api.weixin.qq.com"; var weChatSendNotificationPath = "/cgi-bin/message/subscribe/send"; var requestUrl = BuildRequestUrl(weChatSendNotificationUrl, weChatSendNotificationPath, requestParamters); var responseContent = await MakeRequestAndGetResultAsync(requestUrl, message, cancellationToken); var response = JsonSerializer.Deserialize <SubscribeMessageResponse>(responseContent); if (!response.IsSuccessed) { Logger.LogWarning("Send wechat we app subscribe message failed"); Logger.LogWarning($"Error code: {response.ErrorCode}, message: {response.ErrorMessage}"); } }
public virtual async Task SendAsync( Guid toUser, string templateId, string page = "", string lang = "zh_CN", string state = "formal", Dictionary <string, object> data = null, CancellationToken cancellation = default) { var openId = await UserWeChatOpenIdFinder.FindByUserIdAsync(toUser, AbpWeChatMiniProgramConsts.ProviderKey); if (openId.IsNullOrWhiteSpace()) { Logger.LogWarning("Can not found openId, Unable to send WeChat message!"); return; } var messageData = new SubscribeMessage(openId, templateId, page, state, lang); if (data != null) { messageData.WriteData(data); } await SendAsync(messageData, cancellation); }
protected virtual async Task <string> MakeRequestAndGetResultAsync(string url, SubscribeMessage message, CancellationToken cancellationToken = default) { var client = HttpClientFactory.CreateClient(AbpWeChatMiniProgramConsts.HttpClient); var sendDataContent = JsonSerializer.Serialize(message); var requestContent = new StringContent(sendDataContent); var requestMessage = new HttpRequestMessage(HttpMethod.Post, url) { Content = requestContent }; var response = await client.SendAsync(requestMessage, cancellationToken); if (!response.IsSuccessStatusCode) { throw new AbpException($"WeChat send subscribe message http request service returns error! HttpStatusCode: {response.StatusCode}, ReasonPhrase: {response.ReasonPhrase}"); } var resultContent = await response.Content.ReadAsStringAsync(); return(resultContent); }