public async void Connect() { if (string.IsNullOrWhiteSpace(OTHubSettings.Instance.Telegram?.BotKey) || _botClient != null) { Console.WriteLine("Bot key is null for Telegram."); return; } _botClient = new TelegramBotClient(OTHubSettings.Instance.Telegram.BotKey); _botClient.OnMakingApiRequest += BotClient_OnMakingApiRequest; _botClient.OnApiResponseReceived += BotClient_OnApiResponseReceived; User me = await _botClient.GetMeAsync(); await _botClient.SetMyCommandsAsync(new List <BotCommand>() { //new BotCommand() //{ // Description = "Change Notification Settings", // Command = "settings" //} }, BotCommandScope.AllPrivateChats()); using var cts = new CancellationTokenSource(); _botClient.StartReceiving(new DefaultUpdateHandler(HandleUpdateAsync, HandleErrorAsync), null, cts.Token); IsConnected = true; Console.WriteLine("Telegram bot is connected."); }
/// <summary> /// Returns the list of commands supported by the bot for the given user scope and language; for bots only /// </summary> public static Task <BotCommands> GetCommandsAsync( this Client client, BotCommandScope scope = default, string languageCode = default) { return(client.ExecuteAsync(new GetCommands { Scope = scope, LanguageCode = languageCode })); }
/// <summary> /// Sets the list of commands supported by the bot for the given user scope and language; for bots only /// </summary> public static Task <Ok> SetCommandsAsync( this Client client, BotCommandScope scope = default, string languageCode = default, BotCommand[] commands = default) { return(client.ExecuteAsync(new SetCommands { Scope = scope, LanguageCode = languageCode, Commands = commands })); }
/// <summary>Use this method to delete the list of the bot's commands for the given scope and user language. After deletion, <a href="https://core.telegram.org/bots/api#determining-list-of-commands">higher level commands</a> will be shown to affected users.</summary> /// <param name="bot">BotClient</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users for which the commands are relevant. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode"> A two-letter ISO 639-1 language code. If empty, commands will be applied to all users from the given scope, for whose language there are no dedicated commands.</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <returns>True on success.</returns> public static async Task <bool> DeleteMyCommandsAsync(this BotClient bot, [Optional] BotCommandScope scope, [Optional] string languageCode, CancellationToken cancellationToken) { if (bot == null) { throw new ArgumentNullException(nameof(bot)); } var args = new DeleteMyCommandsArgs(scope, languageCode); return(await bot.RPCA <bool>(MethodNames.DeleteMyCommands, args, cancellationToken : cancellationToken).ConfigureAwait(false)); }
/// <summary>Use this method to delete the list of the bot's commands for the given scope and user language. After deletion, <a href="https://core.telegram.org/bots/api#determining-list-of-commands">higher level commands</a> will be shown to affected users.</summary> /// <param name="bot">BotClient</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users for which the commands are relevant. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode"> A two-letter ISO 639-1 language code. If empty, commands will be applied to all users from the given scope, for whose language there are no dedicated commands.</param> /// <returns>True on success.</returns> public static bool DeleteMyCommands(this BotClient bot, [Optional] BotCommandScope scope, [Optional] string languageCode) { if (bot == null) { throw new ArgumentNullException(nameof(bot)); } var args = new DeleteMyCommandsArgs(scope, languageCode); return(bot.RPC <bool>(MethodNames.DeleteMyCommands, args)); }
/// <summary>Use this method to get the current list of the bot's commands for the given scope and user language.</summary> /// <param name="bot">BotClient</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode">A two-letter ISO 639-1 language code or an empty string.</param> /// <exception cref="BotRequestException">Thrown when a request to Telegram Bot API got an error response.</exception> /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception> /// <returns>Array of <see cref="BotCommand"/> on success. If commands aren't set, an empty list is returned.</returns> public static TValue GetMyCommands <TValue>(this BotClient bot, [Optional] BotCommandScope scope, [Optional] string languageCode) where TValue : IEnumerable <BotCommand> { if (bot == null) { throw new ArgumentNullException(nameof(bot)); } var args = new GetMyCommandsArgs(scope, languageCode); return(bot.RPC <TValue>(MethodNames.GetMyCommands, args)); }
public static async Task <int> RunBot(string?botToken, CancellationToken cancellationToken = default) { var(botConfig, loadBotConfigErrMsg) = await BotConfig.LoadBotConfigAsync(cancellationToken); if (loadBotConfigErrMsg is not null) { Console.WriteLine(loadBotConfigErrMsg); return(1); } // Priority: commandline option > environment variable > config file if (string.IsNullOrEmpty(botToken)) { botToken = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN"); } if (string.IsNullOrEmpty(botToken)) { botToken = botConfig.BotToken; } if (string.IsNullOrEmpty(botToken)) { Console.WriteLine("Please provide a bot token with command line option `--bot-token`, environment variable `TELEGRAM_BOT_TOKEN`, or in the config file."); return(-1); } try { var bot = new TelegramBotClient(botToken); Console.WriteLine("Created Telegram bot instance with API token."); var me = await bot.GetMeAsync(cancellationToken); if (string.IsNullOrEmpty(me.Username)) { throw new Exception("Error: bot username is null or empty."); } await bot.SetMyCommandsAsync(UpdateHandler.BotCommandsPublic, null, null, cancellationToken); Console.WriteLine($"Registered {UpdateHandler.BotCommandsPublic.Length} bot commands for all chats."); var privateChatCommands = UpdateHandler.BotCommandsPrivate.Concat(UpdateHandler.BotCommandsPublic); await bot.SetMyCommandsAsync(privateChatCommands, BotCommandScope.AllPrivateChats(), null, cancellationToken); Console.WriteLine($"Registered {privateChatCommands.Count()} bot commands for private chats."); Console.WriteLine($"Started Telegram bot: @{me.Username} ({me.Id})."); var updateHandler = new UpdateHandler(me.Username, botConfig); var updateReceiver = new QueuedUpdateReceiver(bot, null, UpdateHandler.HandleErrorAsync); await updateHandler.HandleUpdateStreamAsync(bot, updateReceiver, cancellationToken); } catch (ArgumentException ex) { Console.WriteLine($"Invalid access token: {ex.Message}"); } catch (HttpRequestException ex) { Console.WriteLine($"A network error occurred: {ex.Message}"); } catch (Exception ex) { Console.WriteLine(ex); } return(0); }
/// <summary>Use this method to get the current list of the bot's commands for the given scope and user language.</summary> /// <param name="bot">BotClient</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode">A two-letter ISO 639-1 language code or an empty string.</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <exception cref="BotRequestException">Thrown when a request to Telegram Bot API got an error response.</exception> /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception> /// <returns>Array of <see cref="BotCommand"/> on success. If commands aren't set, an empty list is returned. on success. If commands aren't set, an empty list is returned.</returns> public static async Task <TValue> GetMyCommandsAsync <TValue>(this BotClient bot, [Optional] BotCommandScope scope, [Optional] string languageCode, [Optional] CancellationToken cancellationToken) where TValue : IEnumerable <BotCommand> { if (bot == null) { throw new ArgumentNullException(nameof(bot)); } var args = new GetMyCommandsArgs(scope, languageCode); return(await bot.RPCA <TValue>(MethodNames.GetMyCommands, args, cancellationToken : cancellationToken).ConfigureAwait(false)); }
/// <summary>Use this method to get the current list of the bot's commands for the given scope and user language.</summary> /// <param name="bot">BotClient</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode">A two-letter ISO 639-1 language code or an empty string.</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <exception cref="BotRequestException">Thrown when a request to Telegram Bot API got an error response.</exception> /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception> /// <returns>Array of <see cref="BotCommand"/> on success. If commands aren't set, an empty list is returned.</returns> public static async Task <BotCommand[]> GetMyCommandsAsync(this BotClient bot, [Optional] BotCommandScope scope, [Optional] string languageCode, [Optional] CancellationToken cancellationToken) => await bot.GetMyCommandsAsync <BotCommand[]>(scope, languageCode, cancellationToken);
/// <summary>Use this method to get the current list of the bot's commands for the given scope and user language.</summary> /// <param name="bot">BotClient</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode">A two-letter ISO 639-1 language code or an empty string.</param> /// <exception cref="BotRequestException">Thrown when a request to Telegram Bot API got an error response.</exception> /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception> /// <returns>Array of <see cref="BotCommand"/> on success. If commands aren't set, an empty list is returned.</returns> public static BotCommand[] GetMyCommands(this BotClient bot, [Optional] BotCommandScope scope, [Optional] string languageCode) => bot.GetMyCommands <BotCommand[]>(scope, languageCode);
/// <summary>Inititalize a new instance of <see cref="SetMyCommandsArgs"/>.</summary> /// <param name="commands">A <see cref="BotCommand"/> list of bot commands to be set as the list of the bot's commands. At most 100 commands can be specified.</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users for which the commands are relevant. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode">A two-letter ISO 639-1 language code. If empty, commands will be applied to all users from the given scope, for whose language there are no dedicated commands.</param> public SetMyCommandsArgs(IEnumerable <BotCommand> commands, [Optional] BotCommandScope scope, [Optional] string languageCode) { Commands = commands; Scope = scope; LanguageCode = languageCode; }
/// <summary>Use this method to change the list of the bot's commands. See https://core.telegram.org/bots#commands for more details about bot commands. Returns True on success.</summary> /// <param name="bot">BotClient</param> /// <param name="commands">A <see cref="BotCommand"/> list of bot commands to be set as the list of the bot's commands. At most 100 commands can be specified.</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users for which the commands are relevant. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode">A two-letter ISO 639-1 language code. If empty, commands will be applied to all users from the given scope, for whose language there are no dedicated commands.</param> /// <exception cref="BotRequestException">Thrown when a request to Telegram Bot API got an error response.</exception> /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception> /// <returns>True</returns> public static bool SetMyCommands(this BotClient bot, IEnumerable <BotCommand> commands, [Optional] BotCommandScope scope, [Optional] string languageCode) { if (bot == null) { throw new ArgumentNullException(nameof(bot)); } if (commands == default) { throw new ArgumentNullException(nameof(commands)); } var args = new SetMyCommandsArgs(commands, scope, languageCode); return(bot.RPC <bool>(MethodNames.SetMyCommands, args)); }
/// <summary>Use this method to change the list of the bot's commands. See https://core.telegram.org/bots#commands for more details about bot commands. Returns True on success.</summary> /// <param name="bot">BotClient</param> /// <param name="commands">A list of bot commands to be set as the list of the bot's commands. At most 100 commands can be specified.</param> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users for which the commands are relevant. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode">A two-letter ISO 639-1 language code. If empty, commands will be applied to all users from the given scope, for whose language there are no dedicated commands</param> /// <param name="cancellationToken">The cancellation token to cancel operation.</param> /// <exception cref="BotRequestException">Thrown when a request to Telegram Bot API got an error response.</exception> /// <exception cref="ArgumentNullException">Thrown when a required parameter is null.</exception> /// <returns>True</returns> public static async Task <bool> SetMyCommandsAsync(this BotClient bot, IEnumerable <BotCommand> commands, [Optional] BotCommandScope scope, [Optional] string languageCode, [Optional] CancellationToken cancellationToken) { if (bot == null) { throw new ArgumentNullException(nameof(bot)); } if (commands == default) { throw new ArgumentNullException(nameof(commands)); } var args = new SetMyCommandsArgs(commands, scope, languageCode); return(await bot.RPCA <bool>(MethodNames.SetMyCommands, args, cancellationToken).ConfigureAwait(false)); }
/// <summary>Inititalize a new instance of <see cref="GetMyCommandsArgs"/>.</summary> /// <param name="scope">A <see cref="BotCommandScope"/> object, describing scope of users. Defaults to <see cref="BotCommandScopeDefault"/>.</param> /// <param name="languageCode">A two-letter ISO 639-1 language code or an empty string.</param> public GetMyCommandsArgs([Optional] BotCommandScope scope, [Optional] string languageCode) { Scope = scope; LanguageCode = languageCode; }