using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Duplicati.Library.Interface;
namespace Duplicati.Library.Modules.Builtin;
public class SendTelegramMessage : ReportHelper
{
///
/// The tag used for log messages
///
private static readonly string LOGTAG = Logging.Log.LogTagFromType();
///
/// The timeout for the HTTP request
///
private static readonly TimeSpan REQUEST_TIMEOUT = TimeSpan.FromSeconds(10);
#region Option names
///
/// Option used to specify Telegram bot ID
///
private const string OPTION_BOTID = "send-telegram-bot-id";
///
/// Option used to specify Telegram bot API key
///
private const string OPTION_APIKEY = "send-telegram-api-key";
///
/// Option used to specify channel to send to
///
private const string OPTION_CHANNEL = "send-telegram-channel-id";
///
/// Option used to specify report body
///
private const string OPTION_MESSAGE = "send-telegram-message";
///
/// Option used to specify report level
///
private const string OPTION_SENDLEVEL = "send-telegram-level";
///
/// Option used to specify if reports are sent for other operations than backups
///
private const string OPTION_SENDALL = "send-telegram-any-operation";
///
/// Option used to specify what format the result is sent in.
///
private const string OPTION_RESULT_FORMAT = "send-telegram-result-output-format";
///
/// Option used to set the log level
///
private const string OPTION_LOG_LEVEL = "send-telegram-log-level";
///
/// Option used to set the log level
///
private const string OPTION_LOG_FILTER = "send-telegram-log-filter";
///
/// Option used to set the maximum number of log lines
///
private const string OPTION_MAX_LOG_LINES = "send-telegram-max-log-lines";
#endregion
#region Option defaults
///
/// The default message body
///
protected override string DEFAULT_BODY => string.Format("Duplicati %OPERATIONNAME% report for %backup-name%{0}{0} %RESULT%", Environment.NewLine);
///
/// Don't use the subject for telegram
///
protected override string DEFAULT_SUBJECT => string.Empty;
#endregion
#region Implementation of IGenericModule
///
/// The module key, used to activate or deactivate the module on the commandline
///
public override string Key => "sendtelegram";
///
/// A localized string describing the module with a friendly name
///
public override string DisplayName => Strings.SendTelegramMessage.DisplayName;
///
/// A localized description of the module
///
public override string Description => Strings.SendTelegramMessage.Description;
///
/// A boolean value that indicates if the module should always be loaded.
/// If true, the user can choose to not load the module by entering the appropriate commandline option.
/// If false, the user can choose to load the module by entering the appropriate commandline option.
///
public override bool LoadAsDefault => true;
///
/// Gets a list of supported commandline arguments
///
public override IList SupportedCommands
=> [
new CommandLineArgument(OPTION_CHANNEL, CommandLineArgument.ArgumentType.String, Strings.SendTelegramMessage.SendtelegramchannelShort, Strings.SendTelegramMessage.SendtelegramchannelLong),
new CommandLineArgument(OPTION_MESSAGE, CommandLineArgument.ArgumentType.String, Strings.SendTelegramMessage.SendtelegrammessageShort, Strings.SendTelegramMessage.SendtelegrammessageLong, DEFAULT_BODY),
new CommandLineArgument(OPTION_BOTID, CommandLineArgument.ArgumentType.String, Strings.SendTelegramMessage.SendtelegrambotidShort, Strings.SendTelegramMessage.SendtelegrambotidLong),
new CommandLineArgument(OPTION_APIKEY, CommandLineArgument.ArgumentType.String, Strings.SendTelegramMessage.SendtelegramapikeyShort, Strings.SendTelegramMessage.SendtelegramapikeyLong),
new CommandLineArgument(OPTION_SENDLEVEL, CommandLineArgument.ArgumentType.String, Strings.SendTelegramMessage.SendtelegramlevelShort, Strings.SendTelegramMessage.SendtelegramlevelLong(ParsedResultType.Success.ToString(), ParsedResultType.Warning.ToString(), ParsedResultType.Error.ToString(), ParsedResultType.Fatal.ToString(), "All"), DEFAULT_LEVEL, null, Enum.GetNames(typeof(ParsedResultType)).Union(new string[] { "All" } ).ToArray()),
new CommandLineArgument(OPTION_SENDALL, CommandLineArgument.ArgumentType.Boolean, Strings.SendTelegramMessage.SendtelegramanyoperationShort, Strings.SendTelegramMessage.SendtelegramanyoperationLong),
new CommandLineArgument(OPTION_LOG_LEVEL, CommandLineArgument.ArgumentType.Enumeration, Strings.ReportHelper.OptionLoglevelShort, Strings.ReportHelper.OptionLoglevelLong, DEFAULT_LOG_LEVEL.ToString(), null, Enum.GetNames(typeof(Logging.LogMessageType))),
new CommandLineArgument(OPTION_LOG_FILTER, CommandLineArgument.ArgumentType.String, Strings.ReportHelper.OptionLogfilterShort, Strings.ReportHelper.OptionLogfilterLong),
new CommandLineArgument(OPTION_MAX_LOG_LINES, CommandLineArgument.ArgumentType.Integer, Strings.ReportHelper.OptionmaxloglinesShort, Strings.ReportHelper.OptionmaxloglinesLong, DEFAULT_LOGLINES.ToString()),
new CommandLineArgument(OPTION_RESULT_FORMAT, CommandLineArgument.ArgumentType.Enumeration, Strings.ReportHelper.ResultFormatShort, Strings.ReportHelper.ResultFormatLong(Enum.GetNames(typeof(ResultExportFormat))), DEFAULT_EXPORT_FORMAT.ToString(), null, Enum.GetNames(typeof(ResultExportFormat))),
];
protected override string SubjectOptionName => OPTION_MESSAGE;
protected override string BodyOptionName => OPTION_MESSAGE;
protected override string ActionLevelOptionName => OPTION_SENDLEVEL;
protected override string ActionOnAnyOperationOptionName => OPTION_SENDALL;
protected override string LogLevelOptionName => OPTION_LOG_LEVEL;
protected override string LogFilterOptionName => OPTION_LOG_FILTER;
protected override string LogLinesOptionName => OPTION_MAX_LOG_LINES;
protected override string ResultFormatOptionName => OPTION_RESULT_FORMAT;
///
/// The server username
///
private string m_botid;
///
/// The server password
///
private string m_apikey;
///
/// The Telegram ChannelID
///
private string m_channelId;
///
/// This method is the interception where the module can interact with the execution environment and modify the settings.
///
/// A set of commandline options passed to Duplicati
protected override bool ConfigureModule(IDictionary commandlineOptions)
{
//We need at least a recipient
commandlineOptions.TryGetValue(OPTION_CHANNEL, out m_channelId);
if (string.IsNullOrEmpty(m_channelId))
return false;
commandlineOptions.TryGetValue(OPTION_BOTID, out m_botid);
commandlineOptions.TryGetValue(OPTION_APIKEY, out m_apikey);
return true;
}
#endregion
protected override string ReplaceTemplate(string input, object result, Exception exception, bool subjectline)
{
// No need to do the expansion as we throw away the result
if (subjectline)
return string.Empty;
return base.ReplaceTemplate(input, result, exception, subjectline);
}
protected override async void SendMessage(string subject, string body)
{
try
{
var p = new
{
chat_id = Uri.EscapeDataString(m_channelId),
parse_mode = "Text",
text = Uri.EscapeDataString(body),
botId = Uri.EscapeDataString(m_botid),
apiKey = Uri.EscapeDataString(m_apikey)
};
var url = $"https://api.telegram.org/bot{p.botId}:{p.apiKey}/sendMessage?chat_id={p.chat_id}&parse_mode={p.parse_mode}&text={p.text}";
using var client = new HttpClient { Timeout = REQUEST_TIMEOUT };
var response = await client.GetAsync(url);
var responseContent = await response.Content.ReadAsStringAsync();
if (responseContent.Contains("\"ok\":true"))
return;
Logging.Log.WriteWarningMessage(LOGTAG, "telegramSendError", null, "Failed to send to telegram messages: {0}", responseContent);
}
catch (Exception e)
{
Logging.Log.WriteWarningMessage(LOGTAG, "telegramSendError", e, "Failed to send to telegram messages: {0}", e.Message);
}
}
}