示例#1
0
    public async Task SendToMany(IEnumerable <long> chatIds, string message, CoreKeyboard keyboard = null,
                                 IEnumerable <string> attachments = null)
    {
        message = TrimText(message);

        foreach (var chunk in chatIds.Chunk(ChunkLimit))
        {
            try
            {
                await _vkApi.Messages.SendToUserIdsAsync(new MessagesSendParams
                {
                    UserIds     = chunk,
                    Message     = message,
                    Keyboard    = KeyboardConverter.FromCoreToVk(keyboard, true),
                    Attachments = ConvertAttachments(attachments),
                    RandomId    = GetRandomId()
                });

                await Task.Delay(100);
            }
            catch (Exception e)
            {
                _logger.Error(e, "Ошибка при отправке сообщения");
            }
        }
    }
示例#2
0
    private async Task MessageEvent(MessageEvent messageEvent)
    {
        var mappedToMessage = _mapper.Map <Message>(messageEvent);
        await _commandsService.ExecuteCommand(mappedToMessage, OnSuccess, OnFailed);

        async Task OnSuccess(IResult res)
        {
            try
            {
                await _vkApi.Messages.EditAsync(new MessageEditParams()
                {
                    PeerId = messageEvent.PeerId.GetValueOrDefault(0),
                    ConversationMessageId = messageEvent.ConversationMessageId,
                    Keyboard = KeyboardConverter.FromCoreToVk(res.Keyboard, true),
                    Message  = res.Message
                });
            }
            catch
            {
                await _sender.Send(messageEvent.PeerId.GetValueOrDefault(0), res.Message, res.Keyboard);
            }
        }

        async Task OnFailed(IResult res)
        {
            await _vkApi.Messages.SendMessageEventAnswerAsync(messageEvent.EventId,
                                                              messageEvent.UserId.GetValueOrDefault(0),
                                                              messageEvent.PeerId.GetValueOrDefault(0),
                                                              new EventData()
            {
                Type = MessageEventType.SnowSnackbar,
                Text = res.Message
            });
        }
    }
示例#3
0
    public Task Send(long chatId, string message, CoreKeyboard keyboard = null, IEnumerable <string> attachments = null)
    {
        message = TrimText(message);

        return(_vkApi.Messages.SendAsync(new MessagesSendParams
        {
            PeerId = chatId,
            Message = message,
            Keyboard = KeyboardConverter.FromCoreToVk(keyboard, true),
            Attachments = ConvertAttachments(attachments),
            RandomId = GetRandomId()
        }));
    }