Exemplo n.º 1
0
        private async Task <Option <Error> > SendPhotoAsync(TelegramTriggerConfig telegramTrigger, string fullPath,
                                                            CancellationToken cancellationToken)
        {
            // TODO: make this more functional
            var chatIds = _telegramConfig.ChatIds.ToSeq();

            if (chatIds.IsEmpty)
            {
                return(None);
            }

            var client = new TelegramBotClient(_telegramConfig.Token);

            await using var stream = File.OpenRead(fullPath);
            Option <string> fileId = None;
            var             cts    = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            cts.CancelAfter(TimeSpan.FromSeconds(5));
            try
            {
                var message = await client.SendPhotoAsync(chatIds.Head, stream, telegramTrigger.Caption,
                                                          cancellationToken : cts.Token);

                var photos = message.Photo.ToSeq();
                if (!photos.IsEmpty)
                {
                    fileId = photos.Head.FileId;
                }
            }
            catch (Exception ex)
            {
                // we only consider the send to have failed
                // if the initial upload fails since subsequent
                // sends use a file id

                // don't bother returning an error if the child
                // task wasn't canceled (meaning we're shutting down)
                return(cts.IsCancellationRequested
                    ? Some(Error.New(ex))
                    : None);
            }

            await fileId.IfSomeAsync(async f =>
            {
                foreach (var chatId in chatIds.Tail)
                {
                    await client.SendPhotoAsync(chatId, f, telegramTrigger.Caption,
                                                cancellationToken: cancellationToken);
                }
            });

            return(None);
        }
Exemplo n.º 2
0
        public async Task ProcessTriggerAsync(TelegramTriggerConfig telegramTrigger, Image image,
                                              CancellationToken cancellationToken)
        {
            if (!telegramTrigger.IsInCooldown(image))
            {
                var result = await SendPhotoAsync(telegramTrigger, image.FullPath, cancellationToken);

                await result.IfSomeAsync(async _ => await SendTextAsync(telegramTrigger, cancellationToken));

                telegramTrigger.NextTrigger = image.Timestamp + TimeSpan.FromSeconds(telegramTrigger.CooldownSeconds);
            }
        }
Exemplo n.º 3
0
        private async Task SendTextAsync(TelegramTriggerConfig telegramTrigger, CancellationToken cancellationToken)
        {
            var chatIds = _telegramConfig.ChatIds.ToSeq();

            if (!chatIds.IsEmpty)
            {
                var client = new TelegramBotClient(_telegramConfig.Token);
                var cts    = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
                cts.CancelAfter(TimeSpan.FromSeconds(5));
                try
                {
                    await client.SendTextMessageAsync(chatIds.Head, telegramTrigger.Caption,
                                                      cancellationToken : cts.Token);
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex, "Failed to send fallback text to telegram");
                }
            }
        }