Ejemplo n.º 1
0
        /// <summary>
        /// Сканировать входящий Telegram.Update
        /// </summary>
        public void UpdateNew(Update u)
        {
            IncUpdate = u;
            UpdateNew();

            Log.WriteLine((IsAdminSender ? "Administrator" : "User") + FullNameSender, IsAdminSender ? LogStatusEnum.Notice : LogStatusEnum.Norma);

            if (ChatType == ChatTypes.Group || ChatType == ChatTypes.Supergroup)
            {
                Regex rex = new Regex(@"^/\w[\w\d_]+@" + telegram_client.Me.username + "$", RegexOptions.IgnoreCase);
                if (CompositionTypes.Exists(x => x == TelegramDataTypes.Text) && rex.IsMatch(IncUpdate.message.text))
                {
                    telegram_client.sendMessage(IncUpdate.message.chat.id.ToString(), "Для общения с ботом напишите ему напрямую @" + telegram_client.Me.username, "", false, false, IncUpdate.message.chat.id);
                    return;
                }
                if (ScanSpamMatches.Count > 0)
                {
                    NotifyAdmins(MatchesReport);

                    if (AutoDelete && !IsAdminSender && ScanSpamMatches.Exists(x => x.LevelMatch == LevelMatches.Block))
                    {
                        telegram_client.deleteMessage(u.message.chat.id.ToString(), u.message.message_id);
                    }
                }
            }

            if (!string.IsNullOrEmpty(WebhookAddress) && !string.IsNullOrEmpty(hmac_key) && !string.IsNullOrEmpty(hmac_secret))
            {
                string postData = glob_tools.SerialiseJSON(this);
                string responsebody;
                //
                HttpWebRequest request;
                Stream         requestStream;
                request             = (HttpWebRequest)WebRequest.Create(WebhookAddress);
                request.ContentType = "application/json";
                request.Method      = "POST";

                HmacHttpWebRequest hmacHttp = new HmacHttpWebRequest(hmac_key, hmac_secret);
                request = hmacHttp.SignRequest(request);

                requestStream = request.GetRequestStream();
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = byteArray.Length;
                requestStream.Write(byteArray, 0, byteArray.Length);
                requestStream.Close();

                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (!hmacHttp.VerifyResponse(response))
                        {
                            Log.WriteLine("Ответ API Webhook не подписан или подписан не действительной поджписью", LogStatusEnum.Alarm);
                            return;
                        }
                        Log.WriteLine("HMAC API Webhook подпись проверена", LogStatusEnum.Happi);
                        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true))
                        {
                            if (response.StatusCode == HttpStatusCode.OK)
                            {
                                responsebody = reader.ReadToEnd();
                                if (string.IsNullOrEmpty(responsebody))
                                {
                                    Log.WriteLine("API HMAC JSON - вернул пустой ответ", LogStatusEnum.Alarm);
                                }
                                else
                                {
                                    try
                                    {
                                        ResultHmacResponseClass resultHmac = (ResultHmacResponseClass)glob_tools.DeSerialiseJSON(typeof(ResultHmacResponseClass), responsebody);
                                        Log.Write("API HMAC JSON - прочитан ");
                                        Log.WriteLine(resultHmac.status.ToString("g"), resultHmac.status == StatusResult.Ok ? LogStatusEnum.Happi : LogStatusEnum.Alarm);
                                    }
                                    catch (Exception e)
                                    {
                                        Log.WriteLine("API HMAC JSON - ошибка. " + e.Message, LogStatusEnum.Alarm);
                                    }
                                }
                            }
                            else
                            {
                                Log.WriteLine("API HTTP-CODE: " + response.StatusCode.ToString("g") + ". HTTP-DESC: " + response.StatusDescription + ".", LogStatusEnum.Alarm);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.WriteLine("API HMAC JSON - HTTP-ошибка. " + e.Message, LogStatusEnum.Alarm);
                }
            }
        }