Skip to content

Commit

Permalink
Add echo, helloWorld, InlineKeyboard and receiveFiles example
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasweimann committed Sep 23, 2020
0 parents commit 258be6d
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
29 changes: 29 additions & 0 deletions Echo/Echo.cs
Original file line number Diff line number Diff line change
@@ -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 = "<PASTE YOUR BOT TOKEN HERE>";
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();
}
}
}
15 changes: 15 additions & 0 deletions Echo/Echo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>RxTelegram.Bot.Examples.Echo</RootNamespace>
<AssemblyName>RxTelegram.Bot.Examples.Echo</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RxTelegram.Bot" Version="0.0.6" />
<PackageReference Include="System.Reactive.Core" Version="4.4.1" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions HelloWorld/HelloWorld.cs
Original file line number Diff line number Diff line change
@@ -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 = "<PASTE YOUR BOT TOKEN HERE>";

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}"!
});
}
}
}
15 changes: 15 additions & 0 deletions HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>RxTelegram.Bot.Examples.HelloWorld</AssemblyName>
<RootNamespace>RxTelegram.Bot.Examples.HelloWorld</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RxTelegram.Bot" Version="0.0.6" />
<PackageReference Include="System.Reactive.Core" Version="4.4.1" />
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions InlineKeyboard/InlineKeyboard.cs
Original file line number Diff line number Diff line change
@@ -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 = "<PASTE YOUR BOT TOKEN HERE>";

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!"
});
}
}
}
}
15 changes: 15 additions & 0 deletions InlineKeyboard/InlineKeyboard.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>RxTelegram.Bot.Examples.InlineKeyboard</AssemblyName>
<RootNamespace>RxTelegram.Bot.Examples.InlineKeyboard</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RxTelegram.Bot" Version="0.0.6" />
<PackageReference Include="System.Reactive.Core" Version="4.4.1" />
</ItemGroup>

</Project>
Empty file added ReceiveFiles/Output/.gitkeep
Empty file.
61 changes: 61 additions & 0 deletions ReceiveFiles/ReceiveFiles.cs
Original file line number Diff line number Diff line change
@@ -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 = "<PASTE YOUR BOT TOKEN HERE>";

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);
}
}
}
21 changes: 21 additions & 0 deletions ReceiveFiles/ReceiveFiles.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>RxTelegram.Bot.Examples.ReceiveFiles</RootNamespace>
<AssemblyName>RxTelegram.Bot.Examples.ReceiveFiles</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RxTelegram.Bot" Version="0.0.6" />
<PackageReference Include="System.Reactive.Core" Version="4.4.1" />
</ItemGroup>

<ItemGroup>
<Content Include="Output\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions RxTelegram.Bot.Examples.sln
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 258be6d

Please sign in to comment.