コード例 #1
0
        private async Task OnDiscord(SocketMessage msg, CancellationToken ct)
        {
            try
            {
                if (msg.Channel.Id != _chatOptions.DiscordChannelId)
                {
                    return;
                }
                if (msg.Author.Id == _discord.CurrentUser.Id)
                {
                    return;
                }

                _logger.LogDebug($"sending #{msg.Id} to telegram from {msg.Author.Username}");

                await new DiscordMessageReader(msg).Read(_discord).SendAsync(_telegram, _chatOptions.TelegramChatId, msg.Id, ct);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error sending #{msg.Id} from {msg.Author.Username}");

                if (_chatOptions.SendErrorsTo != null)
                {
                    await _telegram.SendTextMessageAsync(_chatOptions.SendErrorsTo,
                                                         $"Id: {msg.Id}\nFrom: {msg.Author.Username}\n<pre>{TelegramUtils.Escape(msg.Content)}</pre>\n\n<pre>{TelegramUtils.Escape(ex.ToString())}</pre>",
                                                         ParseMode.Html, cancellationToken : ct);
                }
            }
        }
コード例 #2
0
        private async void OnTelegram(MessageEventArgs e, CancellationToken ct)
        {
            try
            {
                if (e.Message.Chat.Type == ChatType.Private)
                {
                    await OnTelegramDm(e.Message, ct);

                    return;
                }

                if (e.Message.Chat.Id != _chatOptions.TelegramChatId)
                {
                    return;                                                   // ignore unknown chats
                }
                _logger.LogDebug($"sending #{e.Message.MessageId} to discord from {e.Message.From.GetName()}");

                var(parts, embed) = _telegramConverter.Convert(e.Message);

                foreach (var part in parts)
                {
                    await GetDiscordChannel().SendMessageAsync(part, embed: embed);

                    embed = null;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error sending #{e.Message.MessageId}");

                if (_chatOptions.SendErrorsTo != null)
                {
                    await _telegram.SendTextMessageAsync(_chatOptions.SendErrorsTo,
                                                         $"Id: {e.Message.MessageId}\n<pre>{TelegramUtils.Escape(ex.ToString())}</pre>",
                                                         ParseMode.Html, cancellationToken : ct);

                    await _telegram.ForwardMessageAsync(_chatOptions.SendErrorsTo, _chatOptions.TelegramChatId, e.Message.MessageId, false, ct);
                }
            }
        }
コード例 #3
0
        private async void OnTelegramCallback(CallbackQueryEventArgs e, CancellationToken ct)
        {
            try
            {
                var spoilerId = e.CallbackQuery.Data;
                var(dmsg, text) = await ReadSpoiler(spoilerId);

                if (text.Length > 200)
                {
                    try
                    {
                        _logger.LogDebug($"sending spoiler {spoilerId} to {e.CallbackQuery.From.GetName()} via DM");
                        await SendSpoilerAsDm(e.CallbackQuery.From.Id, dmsg, text, ct);
                    }
                    catch (ChatNotInitiatedException)
                    {
                        _logger.LogDebug($"user {e.CallbackQuery.From.GetName()} hasn't started the conversation yet, deeplinking instead");
                    }
                    await _telegram.AnswerCallbackQueryAsync(e.CallbackQuery.Id, url : $"t.me/{_telegramBotName}?start={spoilerId}", cancellationToken : ct);
                }
                else
                {
                    _logger.LogDebug($"showing spoiler {spoilerId} to {e.CallbackQuery.From.GetName()}");
                    await _telegram.AnswerCallbackQueryAsync(e.CallbackQuery.Id, text, true, cancellationToken : ct);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error showing spoiler for #{e.CallbackQuery.Data}");

                if (_chatOptions.SendErrorsTo != null)
                {
                    await _telegram.SendTextMessageAsync(_chatOptions.SendErrorsTo,
                                                         $"Id: {e.CallbackQuery.Data}\n<pre>{TelegramUtils.Escape(ex.ToString())}</pre>",
                                                         ParseMode.Html, cancellationToken : ct);
                }
            }
        }