Exemple #1
0
        public static async Task <bool> SendMessageAsync(string msg)
        {
            Content.text = msg;
            string result = await HttpUtil.PostJsonAsync(Url, Content);

            if (string.IsNullOrWhiteSpace(result))
            {
                return(false);
            }

            try
            {
                var response = JsonConvert.DeserializeObject <TelegramBotResponse>(result);
                if (response.OK)
                {
                    return(true);
                }

                ExceptionlessUtil.Warn(new Exception(response.Description), "TG消息推送失败");
                return(false);
            }
            catch (Exception ex)
            {
                ExceptionlessUtil.Warn(ex, "TG消息推送失败");
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// 使用 SendGrid 发送邮件 (Google Cloud 支持的服务之一)
        /// </summary>
        /// <param name="apiKey">Sendgrid API key.</param>
        /// <param name="from">From.</param>
        /// <param name="to"></param>
        /// <param name="subject">Subject.</param>
        /// <param name="content">Content.</param>
        /// <param name="contentType">Content type.</param>
        public static async Task MailAsync(string apiKey, string from, IEnumerable <string> to, string subject, string content, MailContentType contentType)
        {
            var sendgrid = new SendGridClient(apiKey);
            var message  = new SendGridMessage
            {
                From             = new EmailAddress(from),
                Personalizations = new List <Personalization> {
                    new Personalization {
                        Tos = to.Select(mailAddr => new EmailAddress(mailAddr)).ToList()
                    }
                },
                Subject = subject
            };

            if (contentType == MailContentType.Plain)
            {
                message.PlainTextContent = content;
            }
            else
            {
                message.HtmlContent = content;
            }

            try
            {
                await sendgrid.SendEmailAsync(message);
            }
            catch (Exception ex)
            {
                ExceptionlessUtil.Warn(ex, "邮件发送失败,请检查配置");
            }
        }
Exemple #3
0
        /// <summary>
        /// 使用Headless Chrome访问给定地址
        /// </summary>
        /// <param name="url">目标地址</param>
        /// <param name="operation">请求URL后等待完成的业务处理操作,如等待异步数据渲染到UI</param>
        /// <param name="script">执行JavaScript脚本(operation完成后执行)</param>
        public async Task Explore(string url, Operation operation, Script script)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                return;
            }

            await Task.Run(() =>
            {
                var watch = new Stopwatch();
                watch.Start();

                Starting?.Invoke(this, new ExploreStartingEventArgs(url));
                try
                {
                    //请求URL
                    _chromeDriver.Navigate().GoToUrl(url);

                    //执行JavaScript
                    if (script != null)
                    {
                        _chromeDriver.ExecuteScript(script.Code, script.Args);
                    }

                    //返回条件和超时时间
                    if (operation?.Condition != null)
                    {
                        var driverWait = new WebDriverWait(_chromeDriver, TimeSpan.FromMilliseconds(operation.Timeout));
                        driverWait.Until(driver => operation.Condition.Invoke(driver));
                        operation.Action?.Invoke(_chromeDriver);
                    }

                    watch.Stop();

                    var threadId   = Thread.CurrentThread.ManagedThreadId;
                    var elapsed    = watch.ElapsedMilliseconds;
                    var pageSource = _chromeDriver.PageSource;

                    Completed?.Invoke(this,
                                      new ExploreCompleteEventArgs(url, _chromeDriver, threadId, elapsed, pageSource));
                }
                catch (Exception ex)
                {
                    ExceptionlessUtil.Warn(ex, $"Browser访问{url}出错");
                }
            });
        }
Exemple #4
0
        private static async Task <string> RequestAsync(string url, object parameter, string method)
        {
            using (var handler = new HttpClientHandler())
            {
                handler.ServerCertificateCustomValidationCallback +=
                    (sender, certificate, chain, sslPolicyErrors) => true;

                using (var http = HttpClientFactory.Create(handler))
                {
                    try
                    {
                        HttpResponseMessage response;
                        if (string.Equals(method, "get", StringComparison.OrdinalIgnoreCase))
                        {
                            response = await http.GetAsync(url);
                        }
                        else if (string.Equals(method, "post", StringComparison.OrdinalIgnoreCase))
                        {
                            var content = new StringContent(JsonConvert.SerializeObject(parameter), Encoding.UTF8,
                                                            "application/json");
                            response = await http.PostAsync(url, content);
                        }
                        else
                        {
                            throw new ArgumentException($"暂不支持{method}请求方式");
                        }

                        response.EnsureSuccessStatusCode();
                        return(await response.Content.ReadAsStringAsync());
                    }
                    catch (Exception ex)
                    {
                        ExceptionlessUtil.Warn(ex, $"网络请求出错{url}");
                        return(null);
                    }
                }
            }
        }
Exemple #5
0
        public static async Task MailAsync(string from, IEnumerable <string> to, string subject, string content, MailContentType contentType, string smtpHost, int smtpPort, string userName, string password)
        {
            var message = new MimeMessage();

            message.From.Add(new MailboxAddress(from));
            to.ToList().ForEach(addr => message.To.Add(new MailboxAddress(addr)));
            message.Subject = subject;

            var builder = new BodyBuilder();

            if (contentType == MailContentType.Plain)
            {
                builder.TextBody = content;
            }
            else
            {
                builder.HtmlBody = content;
            }
            message.Body = builder.ToMessageBody();

            try
            {
                using (var client = new SmtpClient())
                {
                    await client.ConnectAsync(smtpHost, smtpPort, true);

                    await client.AuthenticateAsync(userName, password);

                    await client.SendAsync(message);

                    await client.DisconnectAsync(true);
                }
            }
            catch (Exception ex)
            {
                ExceptionlessUtil.Warn(ex, "邮件发送失败,请检查配置");
            }
        }
Exemple #6
0
        private static async Task <Response> RequestAsync(string url, HttpMethod method = null,
                                                          HttpContent content           = null)
        {
            using (var handler = new HttpClientHandler())
            {
                handler.ServerCertificateCustomValidationCallback +=
                    (sender, certificate, chain, sslPolicyErrors) => true;

                using (var http = HttpClientFactory.Create(handler))
                {
                    try
                    {
                        HttpResponseMessage response;
                        if ((method ?? HttpMethod.Get) == HttpMethod.Get)
                        {
                            response = await http.GetAsync(url);
                        }
                        else if (method == HttpMethod.Post)
                        {
                            response = await http.PostAsync(url, content);
                        }
                        else
                        {
                            throw new ArgumentException($"暂不支持{method}请求方式");
                        }

                        response.EnsureSuccessStatusCode();
                        return(new Response(response.StatusCode, response.Headers, response.Content));
                    }
                    catch (Exception ex)
                    {
                        ExceptionlessUtil.Warn(ex, $"网络请求出错{url}");
                        return(null);
                    }
                }
            }
        }