Пример #1
0
        public async Task <Result> SendAsync(string token, string touser, CustomMessage message)
        {
            if (message == null)
            {
                return(Result.Fail("the message can not be null."));
            }
            try
            {
                string url = $@"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={token}";
                message.SetToUser(touser);
                string      msgJson = message.ToJson();
                HttpContent content = new StringContent(msgJson, Encoding.UTF8, "application/json");
                string      result  = await _client.PostAsync(url, content, _logger);

                if (string.IsNullOrEmpty(result))
                {
                    return(Result.Fail("server may be has error."));
                }
                var jobj = JObject.Parse(result);
                if (jobj.TryGetValue("errcode", out JToken value))
                {
                    int errcode = value.Value <int>();
                    if (errcode == 0)
                    {
                        return(Result.Success());
                    }
                    else
                    {
                        return(Result.Fail(result));
                    }
                }
                else
                {
                    return(Result.Fail("the result can not found errcode property."));
                }
            }
            catch (Exception ex)
            {
                return(Result.Fail(ex.Message));
            }
        }