rewrite for .netcore and .netframework
This commit is contained in:
parent
b498114f48
commit
75f53ca419
37
ConnectorUserTelegram-4.7.1.sln
Normal file
37
ConnectorUserTelegram-4.7.1.sln
Normal file
@ -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
|
||||
@ -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
|
||||
|
||||
76
ConnectorUserTelegram/ConnectorUserTelegram-4.7.1.csproj
Normal file
76
ConnectorUserTelegram/ConnectorUserTelegram-4.7.1.csproj
Normal file
@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{E66A57DD-858A-40E4-8A2F-BEA5129C31F7}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>BlubbFish.Utils.IoT.Connector.User</RootNamespace>
|
||||
<AssemblyName>ConnectorUserTelegram</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.ComponentModel.Composition" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.IO.Compression.FileSystem" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Numerics" />
|
||||
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="Telegram.Bot, Version=13.2.1.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Telegram.Bot.13.2.1\lib\netstandard1.1\Telegram.Bot.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Telegram.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Utils-IoT\Utils-IoT\Utils-IoT-4.7.1.csproj">
|
||||
<Project>{b870e4d5-6806-4a0b-b233-8907eedc5afc}</Project>
|
||||
<Name>Utils-IoT</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@ -1,76 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{E66A57DD-858A-40E4-8A2F-BEA5129C31F7}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>BlubbFish.Utils.IoT.Connector.User</RootNamespace>
|
||||
<AssemblyName>ConnectorUserTelegram</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.ComponentModel.Composition" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.IO.Compression.FileSystem" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Numerics" />
|
||||
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="Telegram.Bot, Version=13.2.1.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Telegram.Bot.13.2.1\lib\netstandard1.1\Telegram.Bot.dll</HintPath>
|
||||
</Reference>
|
||||
<PackageReference Include="Telegram.Bot" Version="18.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Telegram.cs" />
|
||||
<ProjectReference Include="..\..\Utils-IoT\Utils-IoT\Utils-IoT.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Utils-IoT\Utils-IoT\Utils-IoT.csproj">
|
||||
<Project>{b870e4d5-6806-4a0b-b233-8907eedc5afc}</Project>
|
||||
<Name>Utils-IoT</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
|
||||
</Project>
|
||||
@ -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
|
||||
*/
|
||||
@ -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<String, ChatId> chat = new Dictionary<String, ChatId>();
|
||||
|
||||
public override Boolean IsConnected {
|
||||
get {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public override Boolean IsConnected => true;
|
||||
|
||||
public Telegram(Dictionary<String, String> 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<UpdateType>() },
|
||||
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) {
|
||||
private ChatId GetChatId(String chatid) {
|
||||
if(!this.chat.ContainsKey(chatid)) {
|
||||
this.chat.Add(chatid, new ChatId(chatid));
|
||||
}
|
||||
return this.chat[chatid];
|
||||
}
|
||||
|
||||
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<Int64, String, Int64, String>(message.From.Id, message.From.Username, message.Chat.Id, message.ReplyToMessage?.Text), message.Date)));
|
||||
}
|
||||
|
||||
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 {
|
||||
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;
|
||||
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<Int64, String, Int64, String>(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.bot.StopReceiving();
|
||||
this.bot = null;
|
||||
}
|
||||
|
||||
public async override void Send(String message, String[] buttons) {
|
||||
public async void SendWithButtons(String message, String[] buttons, String chatid) {
|
||||
try {
|
||||
KeyboardButton[][] button = new KeyboardButton[(Int32)Math.Ceiling(((Double)buttons.Length)/2)][];
|
||||
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<Int64, String, Int64, String>(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<Int64, String, Int64, String>(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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user