mirror of
https://github.com/Jetsparrow/jetherald.git
synced 2026-01-21 07:56:09 +03:00
Move Telegram-related code into JetHeraldBot.Telegram.cs
This commit is contained in:
parent
46bc808789
commit
65137f7890
73
JetHerald/JetHeraldBot.Telegram.cs
Normal file
73
JetHerald/JetHeraldBot.Telegram.cs
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using JetHerald.Commands;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Args;
|
||||||
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
|
namespace JetHerald
|
||||||
|
{
|
||||||
|
public partial class JetHeraldBot
|
||||||
|
{
|
||||||
|
TelegramBotClient TelegramBot { get; set; }
|
||||||
|
Telegram.Bot.Types.User Me { get; set; }
|
||||||
|
ChatCommandRouter Commands;
|
||||||
|
|
||||||
|
async Task InitTelegram()
|
||||||
|
{
|
||||||
|
if (TelegramConfig.UseProxy)
|
||||||
|
{
|
||||||
|
var httpProxy = new WebProxy(TelegramConfig.ProxyUrl)
|
||||||
|
{ Credentials = new NetworkCredential(TelegramConfig.ProxyLogin, TelegramConfig.ProxyPassword) };
|
||||||
|
TelegramBot = new TelegramBotClient(TelegramConfig.ApiKey, httpProxy);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TelegramBot = new TelegramBotClient(TelegramConfig.ApiKey);
|
||||||
|
}
|
||||||
|
Me = await TelegramBot.GetMeAsync();
|
||||||
|
|
||||||
|
Commands = new ChatCommandRouter(Me.Username, Log);
|
||||||
|
Commands.Add(new CreateTopicCommand(Db), "createtopic");
|
||||||
|
Commands.Add(new DeleteTopicCommand(Db), "deletetopic");
|
||||||
|
Commands.Add(new SubscribeCommand(Db), "subscribe", "sub");
|
||||||
|
Commands.Add(new UnsubscribeCommand(Db), "unsubscribe", "unsub");
|
||||||
|
Commands.Add(new ListCommand(Db), "list");
|
||||||
|
|
||||||
|
HeartbeatCancellation = new();
|
||||||
|
HeartbeatTask = CheckHeartbeats(HeartbeatCancellation.Token);
|
||||||
|
|
||||||
|
TelegramBot.OnMessage += TelegramMessageReceived;
|
||||||
|
TelegramBot.StartReceiving();
|
||||||
|
}
|
||||||
|
|
||||||
|
Task SendMessageToTelegramChannel(NamespacedId chat, string formatted)
|
||||||
|
{
|
||||||
|
var id = long.Parse(chat.Id);
|
||||||
|
return TelegramBot.SendTextMessageAsync(id, formatted);
|
||||||
|
}
|
||||||
|
|
||||||
|
async void TelegramMessageReceived(object sender, MessageEventArgs messageEventArgs)
|
||||||
|
{
|
||||||
|
var msg = messageEventArgs.Message;
|
||||||
|
if (msg == null || msg.Type != MessageType.Text)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var reply = await Commands.Execute(sender, messageEventArgs);
|
||||||
|
if (reply != null)
|
||||||
|
await TelegramBot.SendTextMessageAsync(
|
||||||
|
chatId: msg.Chat.Id,
|
||||||
|
text: reply,
|
||||||
|
replyToMessageId: msg.MessageId);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.LogError(e, "Exception occured during handling of command: " + msg.Text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,13 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Telegram.Bot;
|
|
||||||
using Telegram.Bot.Args;
|
|
||||||
using Telegram.Bot.Types.Enums;
|
|
||||||
|
|
||||||
using JetHerald.Commands;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace JetHerald
|
namespace JetHerald
|
||||||
@ -32,39 +26,12 @@ namespace JetHerald
|
|||||||
ServiceProvider = serviceProvider;
|
ServiceProvider = serviceProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
TelegramBotClient TelegramBot { get; set; }
|
|
||||||
ChatCommandRouter Commands;
|
|
||||||
CancellationTokenSource HeartbeatCancellation;
|
CancellationTokenSource HeartbeatCancellation;
|
||||||
Task HeartbeatTask;
|
Task HeartbeatTask;
|
||||||
Telegram.Bot.Types.User Me { get; set; }
|
|
||||||
|
|
||||||
public async Task Init()
|
public async Task Init()
|
||||||
{
|
{
|
||||||
if (TelegramConfig.UseProxy)
|
await InitTelegram();
|
||||||
{
|
|
||||||
var httpProxy = new WebProxy(TelegramConfig.ProxyUrl)
|
|
||||||
{ Credentials = new NetworkCredential(TelegramConfig.ProxyLogin, TelegramConfig.ProxyPassword) };
|
|
||||||
TelegramBot = new TelegramBotClient(TelegramConfig.ApiKey, httpProxy);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
TelegramBot = new TelegramBotClient(TelegramConfig.ApiKey);
|
|
||||||
}
|
|
||||||
Me = await TelegramBot.GetMeAsync();
|
|
||||||
|
|
||||||
Commands = new ChatCommandRouter(Me.Username, Log);
|
|
||||||
Commands.Add(new CreateTopicCommand(Db), "createtopic");
|
|
||||||
Commands.Add(new DeleteTopicCommand(Db), "deletetopic");
|
|
||||||
Commands.Add(new SubscribeCommand(Db), "subscribe", "sub");
|
|
||||||
Commands.Add(new UnsubscribeCommand(Db), "unsubscribe", "unsub");
|
|
||||||
Commands.Add(new ListCommand(Db), "list");
|
|
||||||
|
|
||||||
HeartbeatCancellation = new();
|
|
||||||
HeartbeatTask = CheckHeartbeats(HeartbeatCancellation.Token);
|
|
||||||
|
|
||||||
TelegramBot.OnMessage += BotOnMessageReceived;
|
|
||||||
TelegramBot.StartReceiving();
|
|
||||||
|
|
||||||
await InitDiscord();
|
await InitDiscord();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,7 +94,7 @@ namespace JetHerald
|
|||||||
{
|
{
|
||||||
if (chat.Namespace == "telegram")
|
if (chat.Namespace == "telegram")
|
||||||
{
|
{
|
||||||
await TelegramBot.SendTextMessageAsync(long.Parse(chat.Id), formatted);
|
await SendMessageToTelegramChannel(chat, formatted);
|
||||||
}
|
}
|
||||||
else if (chat.Namespace == "discord")
|
else if (chat.Namespace == "discord")
|
||||||
{
|
{
|
||||||
@ -136,26 +103,5 @@ namespace JetHerald
|
|||||||
}
|
}
|
||||||
catch (Exception e) { Log.LogError(e, $"Error while sending message \"{formatted}\" to {chat}"); }
|
catch (Exception e) { Log.LogError(e, $"Error while sending message \"{formatted}\" to {chat}"); }
|
||||||
}
|
}
|
||||||
|
|
||||||
async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
|
|
||||||
{
|
|
||||||
var msg = messageEventArgs.Message;
|
|
||||||
if (msg == null || msg.Type != MessageType.Text)
|
|
||||||
return;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var reply = await Commands.Execute(sender, messageEventArgs);
|
|
||||||
if (reply != null)
|
|
||||||
await TelegramBot.SendTextMessageAsync(
|
|
||||||
chatId: msg.Chat.Id,
|
|
||||||
text: reply,
|
|
||||||
replyToMessageId: msg.MessageId);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.LogError(e, "Exception occured during handling of command: " + msg.Text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user