diff --git a/ConnectorUserTelegram-4.7.1.sln b/ConnectorUserTelegram-4.7.1.sln
new file mode 100644
index 0000000..eb24c59
--- /dev/null
+++ b/ConnectorUserTelegram-4.7.1.sln
@@ -0,0 +1,37 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 15
+VisualStudioVersion = 15.0.28307.136
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorUserTelegram-4.7.1", "ConnectorUserTelegram\ConnectorUserTelegram-4.7.1.csproj", "{E66A57DD-858A-40E4-8A2F-BEA5129C31F7}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT-4.7.1", "..\Utils-IoT\Utils-IoT\Utils-IoT-4.7.1.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "litjson_4.7.1", "..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj", "{91A14CD2-2940-4500-8193-56D37EDDDBAA}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Release|Any CPU.Build.0 = Release|Any CPU
+ {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.Build.0 = Release|Any CPU
+ {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {7C826771-09A9-44C6-BC5F-E7E8BABFD2A3}
+ EndGlobalSection
+EndGlobal
diff --git a/ConnectorUserTelegram.sln b/ConnectorUserTelegram.sln
index 312bd00..0f055ae 100644
--- a/ConnectorUserTelegram.sln
+++ b/ConnectorUserTelegram.sln
@@ -1,13 +1,15 @@
Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 15
-VisualStudioVersion = 15.0.28307.136
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorUserTelegram", "ConnectorUserTelegram\ConnectorUserTelegram.csproj", "{E66A57DD-858A-40E4-8A2F-BEA5129C31F7}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConnectorUserTelegram", "ConnectorUserTelegram\ConnectorUserTelegram.csproj", "{47608208-8DFE-4E9D-8F9B-FFD1A82298F2}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils-IoT", "..\Utils-IoT\Utils-IoT\Utils-IoT.csproj", "{B870E4D5-6806-4A0B-B233-8907EEDC5AFC}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils-IoT", "..\Utils-IoT\Utils-IoT\Utils-IoT.csproj", "{E0265DBD-6DA0-4E40-837A-8CB11F374562}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "litjson_4.7.1", "..\..\Librarys\litjson\litjson\litjson_4.7.1.csproj", "{91A14CD2-2940-4500-8193-56D37EDDDBAA}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "..\Utils\Utils\Utils.csproj", "{E8263A61-A7C8-4DDF-9E29-873B80C63C76}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "litjson", "..\..\Librarys\litjson\litjson\litjson.csproj", "{8152C622-E81D-4E52-AB55-686B7AD5B89A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,23 +17,27 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}.Release|Any CPU.Build.0 = Release|Any CPU
- {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {B870E4D5-6806-4A0B-B233-8907EEDC5AFC}.Release|Any CPU.Build.0 = Release|Any CPU
- {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {91A14CD2-2940-4500-8193-56D37EDDDBAA}.Release|Any CPU.Build.0 = Release|Any CPU
+ {47608208-8DFE-4E9D-8F9B-FFD1A82298F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {47608208-8DFE-4E9D-8F9B-FFD1A82298F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {47608208-8DFE-4E9D-8F9B-FFD1A82298F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {47608208-8DFE-4E9D-8F9B-FFD1A82298F2}.Release|Any CPU.Build.0 = Release|Any CPU
+ {E0265DBD-6DA0-4E40-837A-8CB11F374562}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E0265DBD-6DA0-4E40-837A-8CB11F374562}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {E0265DBD-6DA0-4E40-837A-8CB11F374562}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E0265DBD-6DA0-4E40-837A-8CB11F374562}.Release|Any CPU.Build.0 = Release|Any CPU
+ {E8263A61-A7C8-4DDF-9E29-873B80C63C76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E8263A61-A7C8-4DDF-9E29-873B80C63C76}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {E8263A61-A7C8-4DDF-9E29-873B80C63C76}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E8263A61-A7C8-4DDF-9E29-873B80C63C76}.Release|Any CPU.Build.0 = Release|Any CPU
+ {8152C622-E81D-4E52-AB55-686B7AD5B89A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {8152C622-E81D-4E52-AB55-686B7AD5B89A}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {8152C622-E81D-4E52-AB55-686B7AD5B89A}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {8152C622-E81D-4E52-AB55-686B7AD5B89A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
- SolutionGuid = {7C826771-09A9-44C6-BC5F-E7E8BABFD2A3}
+ SolutionGuid = {079546FF-493E-49AC-8637-06AAB35FE782}
EndGlobalSection
EndGlobal
diff --git a/ConnectorUserTelegram/ConnectorUserTelegram-4.7.1.csproj b/ConnectorUserTelegram/ConnectorUserTelegram-4.7.1.csproj
new file mode 100644
index 0000000..c9fbb9c
--- /dev/null
+++ b/ConnectorUserTelegram/ConnectorUserTelegram-4.7.1.csproj
@@ -0,0 +1,76 @@
+
+
+
+
+ Debug
+ AnyCPU
+ {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}
+ Library
+ Properties
+ BlubbFish.Utils.IoT.Connector.User
+ ConnectorUserTelegram
+ v4.7.1
+ 512
+
+
+
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+
+ ..\..\..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll
+ True
+
+
+
+
+ ..\..\..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll
+ True
+
+
+
+
+
+ ..\..\..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll
+ True
+
+
+
+
+
+
+
+ ..\packages\Telegram.Bot.13.2.1\lib\netstandard1.1\Telegram.Bot.dll
+
+
+
+
+
+
+
+
+ {b870e4d5-6806-4a0b-b233-8907eedc5afc}
+ Utils-IoT
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/ConnectorUserTelegram/ConnectorUserTelegram.csproj b/ConnectorUserTelegram/ConnectorUserTelegram.csproj
index 6a97cda..522ec26 100644
--- a/ConnectorUserTelegram/ConnectorUserTelegram.csproj
+++ b/ConnectorUserTelegram/ConnectorUserTelegram.csproj
@@ -1,76 +1,15 @@
-
-
-
+
+
- Debug
- AnyCPU
- {E66A57DD-858A-40E4-8A2F-BEA5129C31F7}
- Library
- Properties
- BlubbFish.Utils.IoT.Connector.User
- ConnectorUserTelegram
- v4.7.1
- 512
-
-
-
- true
- full
- false
- bin\Debug\
- DEBUG;TRACE
- prompt
- 4
-
-
- pdbonly
- true
- bin\Release\
- TRACE
- prompt
- 4
+ netcoreapp3.1
+
-
-
- ..\..\..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll
- True
-
-
-
-
- ..\..\..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll
- True
-
-
-
-
-
- ..\..\..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll
- True
-
-
-
-
-
-
-
- ..\packages\Telegram.Bot.13.2.1\lib\netstandard1.1\Telegram.Bot.dll
-
+
+
-
-
+
-
-
- {b870e4d5-6806-4a0b-b233-8907eedc5afc}
- Utils-IoT
-
-
-
-
-
-
-
-
\ No newline at end of file
+
+
diff --git a/ConnectorUserTelegram/Properties/AssemblyInfo.cs b/ConnectorUserTelegram/Properties/AssemblyInfo.cs
index 93202ab..b7d3e07 100644
--- a/ConnectorUserTelegram/Properties/AssemblyInfo.cs
+++ b/ConnectorUserTelegram/Properties/AssemblyInfo.cs
@@ -1,4 +1,5 @@
-using System.Reflection;
+#if !NETCOREAPP
+using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -34,3 +35,7 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+#endif
+/*
+ * 1.0.0.0 Init
+ */
\ No newline at end of file
diff --git a/ConnectorUserTelegram/Telegram.cs b/ConnectorUserTelegram/Telegram.cs
index 4a2a556..b3737b7 100644
--- a/ConnectorUserTelegram/Telegram.cs
+++ b/ConnectorUserTelegram/Telegram.cs
@@ -1,73 +1,126 @@
using System;
using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+
using BlubbFish.Utils.IoT.Events;
+
using Telegram.Bot;
-using Telegram.Bot.Args;
using Telegram.Bot.Exceptions;
+using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
namespace BlubbFish.Utils.IoT.Connector.User {
public class Telegram : AUserBackend {
- private TelegramBotClient bot;
- private ChatId chat;
+ private readonly TelegramBotClient bot;
+ private readonly CancellationTokenSource cts;
+ private readonly Dictionary chat = new Dictionary();
- public override Boolean IsConnected {
- get {
- return true;
- }
- }
+ public override Boolean IsConnected => true;
public Telegram(Dictionary settings) : base(settings) {
+ this.cts = new CancellationTokenSource();
this.bot = new TelegramBotClient(settings["telegram-key"]);
- this.bot.OnMessage += this.Bot_OnMessage;
this.Connect();
}
- private void Bot_OnMessage(Object sender, MessageEventArgs e) {
- this.NotifyClientIncomming(new UserEvent(e.Message.Text, e.Message.Chat.Id, e.Message.Date));
- }
-
private void Connect() {
- this.bot.StartReceiving();
- this.chat = new ChatId(this.settings["chatid"]);
+ this.bot.StartReceiving(
+ updateHandler: this.HandleUpdateAsync,
+ pollingErrorHandler: this.HandlePollingErrorAsync,
+ receiverOptions: new ReceiverOptions { AllowedUpdates = Array.Empty() },
+ cancellationToken: this.cts.Token
+ );
+ _ = this.bot.GetMeAsync();
+ Console.WriteLine("BlubbFish.Utils.IoT.Connector.User.Telegram.Connect(): Connected to Telegram");
}
- public async override void Send(String message) {
- try {
- Message x = await this.bot.SendTextMessageAsync(this.chat, message, ParseMode.Default, false, false, 0, new ReplyKeyboardRemove());
- this.NotifyClientSending(new UserEvent(x.Text, x.Chat.Id, x.Date));
- } catch(ApiRequestException e) {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(e.Message+" "+e.ErrorCode+" "+e.Parameters);
- Console.ForegroundColor = ConsoleColor.White;
+ private ChatId GetChatId(String chatid) {
+ if(!this.chat.ContainsKey(chatid)) {
+ this.chat.Add(chatid, new ChatId(chatid));
}
-
+ return this.chat[chatid];
}
- public override void Dispose() {
- this.bot.StopReceiving();
- this.bot = null;
+ private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) {
+ if(!(update.Message is { } message)) {
+ return;
+ }
+ if(!(message.Text is { } messageText)) {
+ return;
+ }
+ await Task.Run(() => this.NotifyClientIncomming(new UserEvent(messageText, new Tuple(message.From.Id, message.From.Username, message.Chat.Id, message.ReplyToMessage?.Text), message.Date)));
}
- public async override void Send(String message, String[] buttons) {
+ private Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) {
+ String ErrorMessage = exception switch {
+ ApiRequestException apiRequestException => $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
+ _ => exception.ToString()
+ };
+
+ Helper.WriteError(ErrorMessage);
+ return Task.CompletedTask;
+ }
+
+ public async override void Send(String message) => await Task.Run(() => this.Send(message, this.settings["default-chat"]));
+
+ public async void Send(String message, String chatid) {
try {
- KeyboardButton[][] button = new KeyboardButton[(Int32)Math.Ceiling(((Double)buttons.Length)/2)][];
+ Message sentMessage = await this.bot.SendTextMessageAsync(
+ chatId: this.GetChatId(chatid),
+ text: message,
+ cancellationToken: this.cts.Token,
+ replyMarkup: new ReplyKeyboardRemove()
+ );
+ this.NotifyClientSending(new UserEvent(sentMessage.Text, new Tuple(sentMessage.Chat.Id, "", 0, ""), sentMessage.Date));
+ } catch(ApiRequestException apiRequestException) {
+ Helper.WriteError($"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}");
+ }
+ }
+
+ public async void SendWithButtons(String message, String[] buttons, String chatid) {
+ try {
+ KeyboardButton[][] button = new KeyboardButton[(Int32)Math.Ceiling((Double)buttons.Length/2)][];
for(Int32 i = 0; i < buttons.Length; i++) {
- Int32 j = (Int32)Math.Floor(((Double)i) / 2);
+ Int32 j = (Int32)Math.Floor((Double)i / 2);
if (i % 2 == 0) {
button[j] = new KeyboardButton[(j == button.Length - 1) ? ((buttons.Length % 2 == 0) ? 2 : buttons.Length % 2) : 2];
}
button[j][i % 2] = new KeyboardButton(buttons[i]);
}
- Message x = await this.bot.SendTextMessageAsync(this.chat, message, ParseMode.Default, false, false, 0, new ReplyKeyboardMarkup(button,true,true));
- this.NotifyClientSending(new UserEvent(x.Text, x.Chat.Id, x.Date));
- } catch (ApiRequestException e) {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(e.Message + " " + e.ErrorCode + " " + e.Parameters);
- Console.ForegroundColor = ConsoleColor.White;
+ Message sentMessage = await this.bot.SendTextMessageAsync(
+ chatId: this.GetChatId(chatid),
+ text: message,
+ cancellationToken: this.cts.Token,
+ replyMarkup: new ReplyKeyboardMarkup(button)
+ );
+ this.NotifyClientSending(new UserEvent(sentMessage.Text, new Tuple(sentMessage.Chat.Id, "", 0, ""), sentMessage.Date));
+ } catch (ApiRequestException apiRequestException) {
+ Helper.WriteError($"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}");
}
}
+
+ public async void SendLocationWithText(String title, String text, Double lat, Double lon, String chatid) {
+ try {
+ Message sentMessage = await this.bot.SendVenueAsync(
+ chatId: this.GetChatId(chatid),
+ title: title,
+ address: text,
+ cancellationToken: this.cts.Token,
+ latitude: lat,
+ longitude: lon
+ );
+ this.NotifyClientSending(new UserEvent("{" + lat + ", " + lon + "}" + title + ": " + text, new Tuple(sentMessage.Chat.Id, "", 0, ""), sentMessage.Date));
+ } catch(ApiRequestException apiRequestException) {
+ Helper.WriteError($"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}");
+ }
+ }
+
+ public override void Dispose() {
+ this.cts.Cancel();
+ Console.WriteLine("BlubbFish.Utils.IoT.Connector.User.Telegram.Dispose(): Disconnected from Telegram");
+ }
}
}