Пример #1
0
        private static async System.Threading.Tasks.Task MessageDeletionOnCallback(CallbackQueryEventArgs e)
        {
            if ((DateTime.UtcNow - e.CallbackQuery.Message.Date).TotalHours < 48)
            {
                try
                {
                    await botClient.AnswerCallbackQueryAsync(e.CallbackQuery.Id);

                    await botClient.DeleteMessageAsync(e.CallbackQuery.Message.Chat.Id, e.CallbackQuery.Message.MessageId);
                }
                catch (Telegram.Bot.Exceptions.ApiRequestException ex)
                {
                    Console.WriteLine(ex.ErrorCode == 400
                                                ? $"{string.Format("{0:[HH:mm:ss] dd.MM.yy}", DateTime.Now)} - Message with id {e.CallbackQuery.Message.MessageId} has already been deleted"
                                                : $"{string.Format("{0:[HH:mm:ss] dd.MM.yy}", DateTime.Now)} - Exception :( StackTrace: {ex.StackTrace}");
                }
            }
            else
            {
                await botClient.AnswerCallbackQueryAsync(e.CallbackQuery.Id, "удоли через контекстное меню");

                Console.WriteLine($"{string.Format("{0:[HH:mm:ss] dd.MM.yy}", DateTime.Now)} - Old message deletion attempt {e.CallbackQuery.Message.Date} in chat with {users[e.CallbackQuery.From.Id]}");
                SickLeaveEvent.Clear(sickLeaveEvents[e.CallbackQuery.From.Id]);
                SickLeaveEvent.Clear(dayoffEvents[e.CallbackQuery.From.Id]);
                SickLeaveEvent.Clear(vacationEvents[e.CallbackQuery.From.Id]);
                LeaveRequestEvent.Clear(leaveRequestEvents[e.CallbackQuery.From.Id]);
            }
        }
Пример #2
0
 public static void Clear(SickLeaveEvent ev)
 {
     ev.Name  = "";
     ev.Date  = "";
     ev.Start = new DateTime(1900, 1, 1);
     ev.End   = new DateTime(1900, 1, 1);
 }
Пример #3
0
        static async void Bot_OnCallback(object sender, CallbackQueryEventArgs e)
        {
            if (e.CallbackQuery.Data != null)
            {
                Console.WriteLine($"{string.Format("{0:[HH:mm:ss] dd.MM.yy}", DateTime.Now)} - Received a callback query \"{e.CallbackQuery.Data}\" in chat with {users[e.CallbackQuery.From.Id]}.");

                switch (Regex.Replace(e.CallbackQuery.Data, @"[\d-]", string.Empty))
                {
                case "today":
                case "tomorrow":
                    await MessageDeletionOnCallback(e);

                    var startHoursKeyboard = _kbBuilder.StartHoursKeyboard(e.CallbackQuery.Message.Chat.Id);

                    await botClient.SendTextMessageAsync(
                        chatId : e.CallbackQuery.From.Id,
                        text : $"Со скольки?",
                        replyMarkup : new InlineKeyboardMarkup(startHoursKeyboard)
                        );

                    leaveRequestEvents[e.CallbackQuery.From.Id].Date = e.CallbackQuery.Data;
                    break;

                case "sick_start":

                    await MessageDeletionOnCallback(e);

                    var sickStartTicks = long.Parse(e.CallbackQuery.Data.Substring(10));

                    sickLeaveEvents[e.CallbackQuery.From.Id].Start = new DateTime(sickStartTicks);

                    var sickEndKeyboard = _kbBuilder.CalendarKeyboard(DateTime.UtcNow.AddHours(3), "sick_end");

                    await botClient.SendTextMessageAsync(
                        chatId : e.CallbackQuery.From.Id,
                        text : "До какого дня поставить сыкливого (sick leave)?\nВыбери последний день больничного",
                        replyMarkup : new InlineKeyboardMarkup(sickEndKeyboard)
                        );

                    break;

                case "dayoff_start":

                    await MessageDeletionOnCallback(e);

                    var dayoffStartTicks = long.Parse(e.CallbackQuery.Data.Substring(12));

                    dayoffEvents[e.CallbackQuery.From.Id].Start = new DateTime(dayoffStartTicks);

                    var dayoffEndKeyboard = _kbBuilder.CalendarKeyboard(DateTime.UtcNow.AddHours(3), "dayoff_end");

                    await botClient.SendTextMessageAsync(
                        chatId : e.CallbackQuery.From.Id,
                        text : "До какого дня тебя не будет?\nВыбери последний день day off",
                        replyMarkup : new InlineKeyboardMarkup(dayoffEndKeyboard)
                        );

                    break;

                case "vacation_start":

                    await MessageDeletionOnCallback(e);

                    var vacationStartTicks = long.Parse(e.CallbackQuery.Data.Substring(14));

                    vacationEvents[e.CallbackQuery.From.Id].Start = new DateTime(vacationStartTicks);

                    var vacationEndKeyboard = _kbBuilder.CalendarKeyboard(DateTime.UtcNow.AddHours(3), "vacation_end");

                    await botClient.SendTextMessageAsync(
                        chatId : e.CallbackQuery.From.Id,
                        text : "До какого дня у тебя отпуск?\nВыбери последний отпускной день",
                        replyMarkup : new InlineKeyboardMarkup(vacationEndKeyboard)
                        );

                    break;

                case "sick_end":

                    await MessageDeletionOnCallback(e);

                    var sickEndTicks = long.Parse(e.CallbackQuery.Data.Substring(8));

                    sickLeaveEvents[e.CallbackQuery.From.Id].End  = new DateTime(sickEndTicks);
                    sickLeaveEvents[e.CallbackQuery.From.Id].Name = users[e.CallbackQuery.From.Id];

                    var sickLeave = sickLeaveEvents[e.CallbackQuery.From.Id];


                    var sickEvent = new Event
                    {
                        Summary = $"{sickLeave.Name}'s sick leave",
                        Start   = new EventDateTime
                        {
                            Date = string.Format("{0:yyyy-MM-dd}", sickLeave.Start)
                        },
                        End = new EventDateTime
                        {
                            Date = string.Format("{0:yyyy-MM-dd}", sickLeave.End.AddDays(1))
                        },
                        Description = "Event created using telegram bot"
                    };

                    if (sickLeave.Start <= sickLeave.End)
                    {
                        var recurringEvent = _service.Events.Insert(sickEvent, calendarId).Execute();

                        await botClient.SendTextMessageAsync(
                            chatId : e.CallbackQuery.From.Id,
                            parseMode : ParseMode.Markdown,
                            text : $"Cобытие *[{sickEvent.Summary}]* было успешно создано \nс *{sickLeave.Start.ToShortDateString()}* по *{sickLeave.End.ToShortDateString()}*\n\nПоставил случайно или ошибся? Можно удалить!",
                            replyMarkup : new InlineKeyboardMarkup(_kbBuilder.DelOrConfirmKeyboard(recurringEvent.Id, "sick"))
                            );
                    }
                    else
                    {
                        SickLeaveEvent.Clear(sickLeaveEvents[e.CallbackQuery.From.Id]);
                        await botClient.SendTextMessageAsync(
                            chatId : e.CallbackQuery.From.Id,
                            text : "Время окончания события указано раньше времени начала... попробуй ещё раз /start");
                    }

                    break;

                case "dayoff_end":

                    await MessageDeletionOnCallback(e);

                    var dayoffEndTicks = long.Parse(e.CallbackQuery.Data.Substring(10));

                    dayoffEvents[e.CallbackQuery.From.Id].End  = new DateTime(dayoffEndTicks);
                    dayoffEvents[e.CallbackQuery.From.Id].Name = users[e.CallbackQuery.From.Id];

                    var dayoff = dayoffEvents[e.CallbackQuery.From.Id];


                    var dayoffEvent = new Event
                    {
                        Summary = $"{dayoff.Name}'s day off",
                        Start   = new EventDateTime
                        {
                            Date = string.Format("{0:yyyy-MM-dd}", dayoff.Start)
                        },
                        End = new EventDateTime
                        {
                            Date = string.Format("{0:yyyy-MM-dd}", dayoff.End.AddDays(1))
                        },
                        Description = "Event created using telegram bot"
                    };

                    if (dayoff.Start <= dayoff.End)
                    {
                        var recurringEvent = _service.Events.Insert(dayoffEvent, calendarId).Execute();

                        await botClient.SendTextMessageAsync(
                            chatId : e.CallbackQuery.From.Id,
                            parseMode : ParseMode.Markdown,
                            text : $"Cобытие *[{dayoffEvent.Summary}]* было успешно создано \nс *{dayoff.Start.ToShortDateString()}* по *{dayoff.End.ToShortDateString()}*\n\nПоставил случайно или ошибся? Можно удалить!",
                            replyMarkup : new InlineKeyboardMarkup(_kbBuilder.DelOrConfirmKeyboard(recurringEvent.Id, "dayoff"))
                            );
                    }
                    else
                    {
                        SickLeaveEvent.Clear(dayoffEvents[e.CallbackQuery.From.Id]);
                        await botClient.SendTextMessageAsync(
                            chatId : e.CallbackQuery.From.Id,
                            text : "Время окончания события указано раньше времени начала... попробуй ещё раз /start");
                    }

                    break;

                case "vacation_end":

                    await MessageDeletionOnCallback(e);

                    var vacationEndTicks = long.Parse(e.CallbackQuery.Data.Substring(12));

                    vacationEvents[e.CallbackQuery.From.Id].End  = new DateTime(vacationEndTicks);
                    vacationEvents[e.CallbackQuery.From.Id].Name = users[e.CallbackQuery.From.Id];

                    var vacation = vacationEvents[e.CallbackQuery.From.Id];


                    var vacationEvent = new Event
                    {
                        Summary = $"{vacation.Name}'s vacation",
                        Start   = new EventDateTime
                        {
                            Date = string.Format("{0:yyyy-MM-dd}", vacation.Start)
                        },
                        End = new EventDateTime
                        {
                            Date = string.Format("{0:yyyy-MM-dd}", vacation.End.AddDays(1))
                        },
                        Description = "Event created using telegram bot"
                    };

                    if (vacation.Start <= vacation.End)
                    {
                        var recurringEvent = _service.Events.Insert(vacationEvent, calendarId).Execute();

                        await botClient.SendTextMessageAsync(
                            chatId : e.CallbackQuery.From.Id,
                            parseMode : ParseMode.Markdown,
                            text : $"Cобытие *[{vacationEvent.Summary}]* было успешно создано \nс *{vacation.Start.ToShortDateString()}* по *{vacation.End.ToShortDateString()}*\n\nПоставил случайно или ошибся? Можно удалить!",
                            replyMarkup : new InlineKeyboardMarkup(_kbBuilder.DelOrConfirmKeyboard(recurringEvent.Id, "vacation"))
                            );
                    }
                    else
                    {
                        SickLeaveEvent.Clear(vacationEvents[e.CallbackQuery.From.Id]);
                        await botClient.SendTextMessageAsync(
                            chatId : e.CallbackQuery.From.Id,
                            text : "Время окончания события указано раньше времени начала... попробуй ещё раз /start");
                    }

                    break;

                case "start":
                    //0-16
                    await MessageDeletionOnCallback(e);

                    var endHoursKeyboard = _kbBuilder.EndHoursKeyboard(e.CallbackQuery.Message.Chat.Id);

                    await botClient.SendTextMessageAsync(
                        chatId : e.CallbackQuery.From.Id,
                        text : "И до скольки?",
                        replyMarkup : new InlineKeyboardMarkup(endHoursKeyboard)
                        );

                    leaveRequestEvents[e.CallbackQuery.From.Id].Start = int.Parse(e.CallbackQuery.Data.Replace("start", string.Empty));
                    break;

                case "end":
                    //1-18
                    await MessageDeletionOnCallback(e);

                    leaveRequestEvents[e.CallbackQuery.From.Id].End = int.Parse(e.CallbackQuery.Data.Replace("end", string.Empty));

                    if (users[e.CallbackQuery.From.Id] != null)
                    {
                        leaveRequestEvents[e.CallbackQuery.From.Id].Name = users[e.CallbackQuery.From.Id];
                    }
                    else
                    {
                        LeaveRequestEvent.Clear(leaveRequestEvents[e.CallbackQuery.From.Id]);
                        break;
                    }

                    var nowMsc = DateTime.UtcNow.AddHours(3);

                    var parts     = starts[e.CallbackQuery.From.Id].Split('.');
                    var startHour = int.Parse(parts[0]);
                    var startMin  = int.Parse(parts[1]);

                    var workdayBeginningUtc = new DateTime(
                        nowMsc.Year,
                        nowMsc.Month,
                        nowMsc.Day,
                        startHour - 3,
                        startMin,
                        0,
                        DateTimeKind.Utc);

                    if (leaveRequestEvents[e.CallbackQuery.From.Id].Date == "tomorrow")
                    {
                        workdayBeginningUtc = workdayBeginningUtc.AddDays(1);
                    }

                    var start = workdayBeginningUtc.AddMinutes(leaveRequestEvents[e.CallbackQuery.From.Id].Start * TimeButtonsDiff);
                    var end   = workdayBeginningUtc.AddMinutes(leaveRequestEvents[e.CallbackQuery.From.Id].End * TimeButtonsDiff);

                    var lrEvent = new Event
                    {
                        Summary = $"{leaveRequestEvents[e.CallbackQuery.From.Id].Name}'s leave request",
                        //Location = "Somewhere",
                        Start = new EventDateTime
                        {
                            DateTime = start,
                            TimeZone = "Europe/Moscow"
                        },
                        End = new EventDateTime
                        {
                            DateTime = end,
                            TimeZone = "Europe/Moscow"
                        },
                        Description = "Event created using telegram bot"
                    };

                    if (lrEvent.Start.DateTime < lrEvent.End.DateTime)
                    {
                        if (leaveRequestEvents[e.CallbackQuery.From.Id].Name != "")
                        {
                            var recurringEvent = await _service.Events.Insert(lrEvent, calendarId).ExecuteAsync();

                            var from = $"{start.AddHours(3):HH:mm}";
                            var to   = $"{end.AddHours(3):HH:mm}";

                            var day = (DateTime.UtcNow.AddHours(3).Date == lrEvent.Start.DateTime.Value.Date) ? "Сегодня" : "Завтра";
                            await botClient.SendTextMessageAsync(
                                chatId : e.CallbackQuery.From.Id,
                                parseMode : ParseMode.Markdown,
                                text : $"Cобытие *[{lrEvent.Summary}]* было успешно создано \n{day}, с *{from}* до *{to}*\n\nПоставил случайно или ошибся? Можно удалить!",
                                replyMarkup : new InlineKeyboardMarkup(_kbBuilder.DelOrConfirmKeyboard(recurringEvent.Id, "lr"))
                                );
                        }
                        else
                        {
                            LeaveRequestEvent.Clear(leaveRequestEvents[e.CallbackQuery.From.Id]);
                            await botClient.SendTextMessageAsync(
                                chatId : e.CallbackQuery.From.Id,
                                text : $"Что-то пошло не так... попробуй ещё раз /start");
                        }
                    }
                    else
                    {
                        LeaveRequestEvent.Clear(leaveRequestEvents[e.CallbackQuery.From.Id]);
                        await botClient.SendTextMessageAsync(
                            chatId : e.CallbackQuery.From.Id,
                            text : $"Время окончания события указано раньше, время начала... попробуй ещё раз /start");
                    }

                    break;

                case "пн":
                case "вт":
                case "ср":
                case "чт":
                case "пт":
                case "сб":
                case "вс":
                case "calendar_empty":
                case "calendar_dateRow":
                    await botClient.AnswerCallbackQueryAsync(e.CallbackQuery.Id);

                    break;

                case "ok_show":
                case "ok_deletion":
                case "ok_host":
                case "confirm_lr":
                case "confirm_sick":
                case "confirm_dayoff":
                case "confirm_vacation":
                case "cancel_deletion":
                case "cancel_start_hours":
                case "cancel_end_hours":
                case "cancel_days":
                case "cancel_calendar":

                    await MessageDeletionOnCallback(e);

                    SickLeaveEvent.Clear(sickLeaveEvents[e.CallbackQuery.From.Id]);
                    SickLeaveEvent.Clear(dayoffEvents[e.CallbackQuery.From.Id]);
                    SickLeaveEvent.Clear(vacationEvents[e.CallbackQuery.From.Id]);
                    LeaveRequestEvent.Clear(leaveRequestEvents[e.CallbackQuery.From.Id]);
                    break;

                default:

                    await MessageDeletionOnCallback(e);

                    if (e.CallbackQuery.Data.Substring(0, 6) == "delete")
                    {
                        string eventId = e.CallbackQuery.Data.Substring(6);
                        try
                        {
                            _service.Events.Delete(calendarId, eventId).Execute();
                            await botClient.SendTextMessageAsync(
                                chatId : e.CallbackQuery.From.Id,
                                text : $"Событие удалено!");
                        }
                        catch (Google.GoogleApiException ex)
                        {
                            if (ex.Error.Code == 410)
                            {
                                await botClient.SendTextMessageAsync(
                                    chatId : e.CallbackQuery.From.Id,
                                    text : "Событие уже было удалено!");

                                Console.WriteLine($"{string.Format("{0:[HH:mm:ss] dd.MM.yy}", DateTime.Now)} - Event with id {eventId} has already been deleted");
                            }
                            else
                            {
                                Console.WriteLine($"{string.Format("{0:[HH:mm:ss] dd.MM.yy}", DateTime.Now)} - Exeption: {ex.Error.Message}, {ex.Error.Code}");
                            }
                        }
                    }
                    else
                    {
                        await botClient.SendTextMessageAsync(
                            chatId : e.CallbackQuery.From.Id,
                            text : "Я не знаю такой команды, смотри /help"
                            );
                    }

                    SickLeaveEvent.Clear(sickLeaveEvents[e.CallbackQuery.From.Id]);
                    SickLeaveEvent.Clear(dayoffEvents[e.CallbackQuery.From.Id]);
                    SickLeaveEvent.Clear(vacationEvents[e.CallbackQuery.From.Id]);
                    LeaveRequestEvent.Clear(leaveRequestEvents[e.CallbackQuery.From.Id]);
                    break;
                }
            }
        }