コード例 #1
0
 public void OnMessage(object sender, MessageEventArgs e)
 {
     if (e.Message.Text == null)
     {
         return;
     }
     if (e.Message.Text.Contains("ID"))
     {
         var id = int.Parse(Regex.Match(e.Message.Text, @"ID([0-9]+)").Groups[1].Value);
         System.Threading.Tasks.Task <Message> rmId = botClient.SendTextMessageAsync(new ChatId(e.Message.Chat.Id), "Loading Poll.", replyMarkup: new ReplyKeyboardRemove());
         botClient.DeleteMessageAsync(new ChatId(e.Message.Chat.Id), rmId.Result.MessageId);
         System.Threading.Tasks.Task <Message> MsgId = botClient.SendTextMessageAsync(new ChatId(e.Message.Chat.Id), "Loading Poll...");
         try {
             database.UpdatePollMessageId(id, MsgId.Result.MessageId);
             Poll poll = database.GetPoll(id);
             InlineKeyboardMarkup markup = Program.GetReplyMarkUp(e.Message.Chat.Id, poll.Id);
             botClient.EditMessageTextAsync(new ChatId(e.Message.Chat.Id), (int)poll.MessageId, Program.GetText(id), replyMarkup: markup, parseMode: ParseMode.Html);
         } catch (NullReferenceException exc) {
             botClient.EditMessageTextAsync(new ChatId(e.Message.Chat.Id), MsgId.Result.MessageId, $"Can't load the poll, reason:\n{exc.Message}");
         }
     }
 }
コード例 #2
0
        public void OnCallback(object sender, CallbackQueryEventArgs e)
        {
            var senderId = e.CallbackQuery.From.Id;

            Program.DecodeInlineQuery(e.CallbackQuery.Data, out EVote choice, out var chatId, out var pollId);
            Poll poll  = database.GetPoll(pollId);
            var  votes = database.GetVotesInPollFrom(senderId, pollId).ToList();

            if (poll.EventDate < DateTime.Now)
            {
                botClient.AnswerCallbackQueryAsync(e.CallbackQuery.Id, text: "Questo poll è chiuso");
                botClient.EditMessageTextAsync(new ChatId(chatId), (int)poll.MessageId, Program.GetText(pollId), replyMarkup: null, parseMode: ParseMode.Html);
            }
            else
            {
                if (votes.Count == 1)
                {
                    var id = votes[0].Id;
                    database.EditVote(id, choice);
                    botClient.EditMessageTextAsync(new ChatId(chatId), (int)poll.MessageId, Program.GetText(pollId), replyMarkup: Program.GetReplyMarkUp(chatId, pollId), parseMode: ParseMode.Html);
                    botClient.AnswerCallbackQueryAsync(e.CallbackQuery.Id, text: "Il tuo voto è stato modificato");
                }
                else if (votes.Count == 0 || votes == null)
                {
                    database.AddVote(choice, pollId, senderId, e.CallbackQuery.From.FirstName);
                    botClient.EditMessageTextAsync(new ChatId(chatId), (int)poll.MessageId, Program.GetText(pollId), replyMarkup: Program.GetReplyMarkUp(chatId, pollId), parseMode: ParseMode.Html);
                    botClient.AnswerCallbackQueryAsync(e.CallbackQuery.Id, text: "Il tuo voto è stato aggiunto");
                }
                else
                {
                    Console.WriteLine("DATABASE ERROR. Run with debug build to see more informations...");
#if DEBUG
                    Console.WriteLine("ERROR: Something is wrong on the DB! There are 2 or more votes from the same user in a poll!");
                    Console.WriteLine(string.Format(" PollID: {0}\n UserID: {1}", pollId, senderId));
#endif
                }
            }
        }