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

111 lines
4.2 KiB
C#

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.Model.Attachments;
using VkNet.Enums.SafetyEnums;
using System.Collections.ObjectModel;
using System.Threading;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using SvetoforVKBot.Models.Commands.LK;
using System.Linq;
namespace SvetoforVKBot.Models.Commands.LK.EditData
{
public class GetEditPhoneCommand
{
public bool 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 row = new List<MessageKeyboardButton>();
var listButtons = new List<ReadOnlyCollection<MessageKeyboardButton>>();
var keyboardBuilder = new KeyboardBuilder().Clear();
try
{
string phone = regex.Replace(update.@object.message.text, "");
if (phone.Length == 11)
if (phone.StartsWith("8") || phone.StartsWith("7"))
{
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.tag = "Личный кабинет - Данные";
user.phone = JsonConvert.SerializeObject(jsPhones);
SelectEditDataCommand selectEditData = new SelectEditDataCommand();
selectEditData.Execute(update, client, db);
}
else
@params.Message = "Контактный телефон в формате 89123456789 (11 цифр, без плюса и через \"8\" или \"7\" обязательно).";
else
@params.Message = "Контактный телефон в формате 89123456789 (11 цифр, без плюса и через \"8\" или \"7\" обязательно).";
@params.Keyboard = keyboardBuilder.Build();
@params.UserId = chatId;
@params.RandomId = GetRandomId();
client.Messages.SendAsync(@params);
}
catch (Exception ee)
{
@params.Message = "‼Ошибка в GetEditPhoneCommand: " + ee.Message;
@params.Attachments = null;
@params.Keyboard = null;
@params.UserId = 59111081;
@params.RandomId = GetRandomId();
client.Messages.SendAsync(@params);
return true;
}
return true;
}
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
public int GetRandomId()
{
var intBytes = new byte[4];
Rng.GetBytes(intBytes);
return BitConverter.ToInt32(intBytes, 0);
}
}
}