Files
Parasha/SvetoforVKBot/Models/Commands/GetPhoneRecallMeCommand.cs
2021-10-19 06:04:52 +03:00

118 lines
4.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using SvetoforVKBot.Models.Updates;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using VkNet;
using VkNet.Model.RequestParams;
using System.Security.Cryptography;
using VkNet.Model.Keyboard;
using VkNet.Enums.SafetyEnums;
using System.Threading;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.Linq;
namespace SvetoforVKBot.Models.Dop
{
public class GetPhoneRecallMeCommand
{
private const int MODER = 59111081;
public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
{
MessagesSendParams @params = new MessagesSendParams();
MessagesSendParams @params2 = new MessagesSendParams();
var chatId = update.@object.message.from_id;
Regex regex = new Regex("[^0-9]");
int i = 0;
var keyboardBuilder = new KeyboardBuilder().Clear();
try
{
string phone = regex.Replace(update.@object.message.text, "");
if (phone.Length == 11)
if (phone.StartsWith("8"))
{
var user = db.Users.Single(usr => usr.chatId == chatId);
var jsPhones = JsonConvert.DeserializeObject<List<string>>(user.phone);
if (jsPhones.Contains(phone))
{
jsPhones[jsPhones.FindIndex(p => p.Equals(phone))] = jsPhones[0];
jsPhones[0] = phone;
}
else
{
if (jsPhones.Count > 0)
{
for (i = jsPhones.Count; i > 0; i--)
{
if (i != 4)
{
if (i == jsPhones.Count)
jsPhones.Add("");
jsPhones[i] = jsPhones[i - 1];
}
}
}
else
jsPhones.Add("");
jsPhones[0] = phone;
}
user.phone = JsonConvert.SerializeObject(jsPhones);
user.tag = "Заказ обратного звонка";
db.SaveChanges();
keyboardBuilder
.AddButton("🏠 В начало", "startPL", KeyboardButtonColor.Positive);
@params.Keyboard = keyboardBuilder.Build();
@params.Message = "✅Спасибо за заявку! \n" +
"\n" +
"Менеджер перезвонит Вам в ближайшее время и ответит на Ваши вопросы.";
Thread.Sleep(1000);
@params2.Message = "🎉Заказ обратного звонка. \n";
List<long> listFrwMsg = new List<long>() { update.@object.message.id };
@params2.UserId = MODER;
@params2.ForwardMessages = listFrwMsg;
@params2.RandomId = GetRandomId();
client.Messages.Send(@params2);
}
else
@params.Message = "Контактный телефон в формате 89123456789 (11 цифр, без плюса и через \"8\" обязательно).";
else
@params.Message = "Контактный телефон в формате 89123456789 (11 цифр, без плюса и через \"8\" обязательно).";
@params.UserId = chatId;
@params.RandomId = GetRandomId();
client.Messages.SendAsync(@params);
}
catch (Exception ee)
{
@params.Message = "‼Ошибка в GetPhoneRecallMeCommand: " + ee.Message;
@params.Attachments = null;
@params.Keyboard = null;
@params.UserId = 59111081;
@params.RandomId = GetRandomId();
client.Messages.SendAsync(@params);
}
}
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
private int GetRandomId()
{
var intBytes = new byte[4];
Rng.GetBytes(intBytes);
return BitConverter.ToInt32(intBytes, 0);
}
}
}