diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/Echo/Echo.cs b/Echo/Echo.cs new file mode 100644 index 0000000..cf9866d --- /dev/null +++ b/Echo/Echo.cs @@ -0,0 +1,29 @@ +using System; +using System.Threading.Tasks; +using RxTelegram.Bot.Interface.BaseTypes.Requests.Messages; + +namespace RxTelegram.Bot.Examples.Echo +{ + public static class Echo + { + private const string BotToken = ""; + public static async Task Main() + { + var bot = new TelegramBot($"{BotToken}"); + var me = await bot.GetMe(); + Console.WriteLine($"Bot name: @{me.Username}"); + + var subscription = bot.Updates.Message.Subscribe(x => + { + bot.SendMessage(new SendMessage + { + ChatId = x.Chat.Id, + Text = x.Text + }); + }, + exception => Console.WriteLine($"An error has occured: {exception.Message}")); + Console.ReadLine(); + subscription.Dispose(); + } + } +} \ No newline at end of file diff --git a/Echo/Echo.csproj b/Echo/Echo.csproj new file mode 100644 index 0000000..2ef32d4 --- /dev/null +++ b/Echo/Echo.csproj @@ -0,0 +1,15 @@ + + + + Exe + netcoreapp3.1 + RxTelegram.Bot.Examples.Echo + RxTelegram.Bot.Examples.Echo + + + + + + + + diff --git a/HelloWorld/HelloWorld.cs b/HelloWorld/HelloWorld.cs new file mode 100644 index 0000000..9010615 --- /dev/null +++ b/HelloWorld/HelloWorld.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading.Tasks; +using RxTelegram.Bot.Interface.BaseTypes; +using RxTelegram.Bot.Interface.BaseTypes.Requests.Messages; + +namespace RxTelegram.Bot.Examples.HelloWorld +{ + public static class HelloWorld + { + private static TelegramBot Bot { get; set; } + private const string BotToken = ""; + + public static async Task Main() + { + Bot = new TelegramBot($"{BotToken}"); + var me = await Bot.GetMe(); + Console.WriteLine($"Bot name: @{me.Username}"); + + var subscription = Bot.Updates.Message.Subscribe(HandleReceivedMessage); + Console.ReadLine(); + subscription.Dispose(); + } + + private static void HandleReceivedMessage(Message message) + { + Console.WriteLine($"{message.From.Username}: {message.Text}"); + Bot.SendMessage(new SendMessage + { + ChatId = message.Chat.Id, + Text = $"Hello World, {message.From.FirstName ?? message.From.Username}"! + }); + } + } +} \ No newline at end of file diff --git a/HelloWorld/HelloWorld.csproj b/HelloWorld/HelloWorld.csproj new file mode 100644 index 0000000..cdc7b18 --- /dev/null +++ b/HelloWorld/HelloWorld.csproj @@ -0,0 +1,15 @@ + + + + Exe + netcoreapp3.1 + RxTelegram.Bot.Examples.HelloWorld + RxTelegram.Bot.Examples.HelloWorld + + + + + + + + diff --git a/InlineKeyboard/InlineKeyboard.cs b/InlineKeyboard/InlineKeyboard.cs new file mode 100644 index 0000000..48df1d4 --- /dev/null +++ b/InlineKeyboard/InlineKeyboard.cs @@ -0,0 +1,77 @@ +using System; +using System.Threading.Tasks; +using RxTelegram.Bot.Interface.BaseTypes; +using RxTelegram.Bot.Interface.BaseTypes.Requests.Callbacks; +using RxTelegram.Bot.Interface.BaseTypes.Requests.Messages; + +namespace RxTelegram.Bot.Examples.InlineKeyboard +{ + internal static class InlineKeyboard + { + private static TelegramBot Bot { get; set; } + private const string BotToken = ""; + + public static async Task Main() + { + Bot = new TelegramBot($"{BotToken}"); + var me = await Bot.GetMe(); + Console.WriteLine($"Bot name: @{me.Username}"); + + var subscriptionMessage = Bot.Updates.Message.Subscribe(HandleReceivedMessage, + exception => Console.WriteLine($"An error has occured: {exception.Message}")); + var subscriptionCallbackQuery = Bot.Updates.CallbackQuery.Subscribe(HandleCallbackQuery, + exception => Console.WriteLine($"An error has occured: {exception.Message}")); + Console.ReadLine(); + subscriptionMessage.Dispose(); + subscriptionCallbackQuery.Dispose(); + } + + private static void HandleCallbackQuery(CallbackQuery callbackQuery) + { + Bot.AnswerCallbackQuery(new AnswerCallbackQuery + { + Text = callbackQuery.Data, + CallbackQueryId = callbackQuery.Id + }); + } + + private static void HandleReceivedMessage(Message message) + { + if (message.Text.Length != 0) + { + Bot.SendMessage(new SendMessage + { + ChatId = message.Chat.Id, + ReplyMarkup = new InlineKeyboardMarkup + { + InlineKeyboard = new[] + { + new[] + { + new InlineKeyboardButton + { + CallbackData = "You clicked on Abc", + Text = "Abc" + }, + }, + new[] + { + new InlineKeyboardButton + { + CallbackData = "You clicked on 1", + Text = "1" + }, + new InlineKeyboardButton + { + CallbackData = "You clicked on 2", + Text = "2" + } + } + } + }, + Text = "Here is a inline keyboard!" + }); + } + } + } +} \ No newline at end of file diff --git a/InlineKeyboard/InlineKeyboard.csproj b/InlineKeyboard/InlineKeyboard.csproj new file mode 100644 index 0000000..933c891 --- /dev/null +++ b/InlineKeyboard/InlineKeyboard.csproj @@ -0,0 +1,15 @@ + + + + Exe + netcoreapp3.1 + RxTelegram.Bot.Examples.InlineKeyboard + RxTelegram.Bot.Examples.InlineKeyboard + + + + + + + + diff --git a/ReceiveFiles/Output/.gitkeep b/ReceiveFiles/Output/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/ReceiveFiles/ReceiveFiles.cs b/ReceiveFiles/ReceiveFiles.cs new file mode 100644 index 0000000..8733466 --- /dev/null +++ b/ReceiveFiles/ReceiveFiles.cs @@ -0,0 +1,61 @@ +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using RxTelegram.Bot.Api; +using RxTelegram.Bot.Interface.BaseTypes; +using RxTelegram.Bot.Interface.BaseTypes.Requests.Messages; +using File = System.IO.File; + +namespace RxTelegram.Bot.Examples.ReceiveFiles +{ + internal static class ReceiveFiles + { + private static TelegramBot Bot { get; set; } + private const string BotToken = ""; + + public static async Task Main() + { + Bot = new TelegramBot($"{BotToken}"); + var me = await Bot.GetMe(); + Console.WriteLine($"Bot name: @{me.Username}"); + + var subscription = Bot.Updates.Message.Subscribe(HandleReceivedMessage, + exception => Console.WriteLine($"An error has occured: {exception.Message}")); + Console.ReadLine(); + subscription.Dispose(); + } + + private static void HandleReceivedMessage(Message message) + { + RxTelegram.Bot.Interface.BaseTypes.File file = null; + if (message.Photo != null && message.Photo.Any()) + { + var photo = message.Photo.OrderBy(x => x.Height).FirstOrDefault(); + if (photo != null) file = Bot.GetFile(photo.FileId).Result; + } + else if (message.Document != null) + { + file = Bot.GetFile(message.Document.FileId).Result; + } + else if (message.Video != null) + { + file = Bot.GetFile(message.Video.FileId).Result; + } + else if (message.Audio != null) + { + file = Bot.GetFile(message.Audio.FileId).Result; + } + + if (file == null) + { + Bot.SendMessage(new SendMessage {ChatId = message.Chat.Id, Text = "Please send me a file!"}); + return; + } + var photoData = Bot.DownloadFileByteArray(file).Result; + var filename = file.FilePath.Split("/").LastOrDefault(); + File.WriteAllBytes( + Path.Combine(Environment.CurrentDirectory, $"Output/{filename}"), photoData); + } + } +} \ No newline at end of file diff --git a/ReceiveFiles/ReceiveFiles.csproj b/ReceiveFiles/ReceiveFiles.csproj new file mode 100644 index 0000000..1b8a3ee --- /dev/null +++ b/ReceiveFiles/ReceiveFiles.csproj @@ -0,0 +1,21 @@ + + + + Exe + netcoreapp3.1 + RxTelegram.Bot.Examples.ReceiveFiles + RxTelegram.Bot.Examples.ReceiveFiles + + + + + + + + + + Always + + + + diff --git a/RxTelegram.Bot.Examples.sln b/RxTelegram.Bot.Examples.sln new file mode 100644 index 0000000..7e9a820 --- /dev/null +++ b/RxTelegram.Bot.Examples.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloWorld", "HelloWorld\HelloWorld.csproj", "{FAEEE093-E981-467E-A6EB-B8A0B8AC6B49}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Echo", "Echo\Echo.csproj", "{19275F44-0A2B-47ED-83C9-345C001AF12E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReceiveFiles", "ReceiveFiles\ReceiveFiles.csproj", "{078EB8F1-7401-4BAE-886B-A1FDB5201704}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InlineKeyboard", "InlineKeyboard\InlineKeyboard.csproj", "{1ACB1B77-69E4-435A-B0F8-7783B143D6D2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FAEEE093-E981-467E-A6EB-B8A0B8AC6B49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FAEEE093-E981-467E-A6EB-B8A0B8AC6B49}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FAEEE093-E981-467E-A6EB-B8A0B8AC6B49}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FAEEE093-E981-467E-A6EB-B8A0B8AC6B49}.Release|Any CPU.Build.0 = Release|Any CPU + {19275F44-0A2B-47ED-83C9-345C001AF12E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {19275F44-0A2B-47ED-83C9-345C001AF12E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {19275F44-0A2B-47ED-83C9-345C001AF12E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {19275F44-0A2B-47ED-83C9-345C001AF12E}.Release|Any CPU.Build.0 = Release|Any CPU + {078EB8F1-7401-4BAE-886B-A1FDB5201704}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {078EB8F1-7401-4BAE-886B-A1FDB5201704}.Debug|Any CPU.Build.0 = Debug|Any CPU + {078EB8F1-7401-4BAE-886B-A1FDB5201704}.Release|Any CPU.ActiveCfg = Release|Any CPU + {078EB8F1-7401-4BAE-886B-A1FDB5201704}.Release|Any CPU.Build.0 = Release|Any CPU + {1ACB1B77-69E4-435A-B0F8-7783B143D6D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1ACB1B77-69E4-435A-B0F8-7783B143D6D2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1ACB1B77-69E4-435A-B0F8-7783B143D6D2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1ACB1B77-69E4-435A-B0F8-7783B143D6D2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal