Exemplo n.º 1
0
        public override async Task ExecuteAsync(Message message, TelegramBotClient client)
        {
            var chatId = message.Chat.Id;

            await client.SendChatActionAsync(chatId, ChatAction.Typing);

            using (TelegramChatContext db = new TelegramChatContext())
            {
                try
                {
                    var secretEvent = await db.Events.FirstOrDefaultAsync(x => x.HostChatId == chatId);

                    if (secretEvent != null)
                    {
                        var telegramChatsIds = secretEvent.Participants.Select(x => x.Id).ToArray();
                        if (telegramChatsIds.Length >= minimumParticipantsCount)
                        {
                            telegramChatsIds.Shuffle();

                            for (int i = 0; i < telegramChatsIds.Count(); i++)
                            {
                                var chat = secretEvent.Participants.ElementAt(i);
                                chat.GiftUser = secretEvent.Participants.First(x => x.Id == telegramChatsIds[i]);
                            }

                            await client.SendTextMessageAsync(chatId, "The Magic Hat completed its work successfully! Each participant will" +
                                                              " automatically receive a message with the name of the person to whom he is giving the gift.");

                            await MessageDistributor.SendParticipantsMessageAsync(message, client);
                        }
                        else
                        {
                            await client.SendTextMessageAsync(chatId, $"Sorry, to use this command, there must be at least {minimumParticipantsCount} participants.");
                        }
                    }
                    else
                    {
                        await client.SendTextMessageAsync(chatId, @"You have to start registration using command /register.");
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine($"{nameof(ShuffleCommand)}:{e.Message}");
                    if (e.InnerException != null)
                    {
                        Debug.WriteLine(e.InnerException);
                    }
                }
            }
        }
Exemplo n.º 2
0
 public MessageConsumerProvider(string appCode, string metaServer)
 {
     try
     {
         if (Interlocked.CompareExchange(ref _inited, 1, 0) != 0)
         {
             throw new Exception("MessageConsumerProvider对象很重,请确保一个应用里只有实例");
         }
         _distributor = new MessageDistributor(appCode, metaServer);
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
         throw;
     }
     finally
     {
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Unsubscribes all subscriptions of the given type
 /// </summary>
 /// <typeparam name="TMessage">The type of message/event to unsubscribe to</typeparam>
 public void UnsubscribeAll<TMessage>()
 {
     MessageDistributor<TMessage>.UnsubscribeAll();
 }
Exemplo n.º 4
0
 /// <summary>
 /// Publishes a message/event to subscribers using the <see cref="DefaultPublishMethod"/> specified globally for the <see cref="MessageBus"/>
 /// </summary>
 /// <typeparam name="TMessage">The type of message/event to publish</typeparam>
 /// <param name="message">The message/event to publish</param>
 public void Publish<TMessage>(TMessage message)
 {
     MessageDistributor<TMessage>.Publish(message, DefaultPublishMethod);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Publishes a message/event to subscribers using the <see cref="PublishMethod"/> specified
 /// </summary>
 /// <typeparam name="TMessage">The type of message/event to publish</typeparam>
 /// <param name="message">The message/event to publish</param>
 /// <param name="method">The <see cref="PublishMethod"/> to use when publishing the message to subscribers</param>
 public void Publish<TMessage>(TMessage message, PublishMethod method)
 {
     MessageDistributor<TMessage>.Publish(message, method);
 }