Files
i2p 773d05f8f1
Pulsar .NET 9.0 Windows Release / build (push) Waiting to run
Mirror to Codeberg and Gitea / mirror (push) Waiting to run
initial commit
2026-08-27 10:57:58 -06:00

67 lines
2.7 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Pulsar.Server.TelegramSender
{
internal class Send
{
public static async Task<string> SendConnectionMessage(string botToken, string chatId, string clientName, string ipAddress, string country)
{
try
{
string message = $"✨ *New Client Connected!* ✨\n" +
$"👤 Name: {clientName}\n" +
$"🌐 IP: {ipAddress}\n" +
$"🌎 Country: {country}";
string url = $"https://api.telegram.org/bot{botToken}/sendMessage?chat_id={chatId}&text={Uri.EscapeDataString(message)}";
Debug.WriteLine($"Request URL: {url}");
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.Timeout = 10000;
using (WebResponse response = await request.GetResponseAsync())
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
if (httpResponse.StatusCode != HttpStatusCode.OK)
{
throw new Exception($"Telegram API returned status: {httpResponse.StatusCode}");
}
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
string responseText = await reader.ReadToEndAsync();
Debug.WriteLine($"Response: {responseText}");
return responseText;
}
}
}
catch (WebException ex)
{
if (ex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)ex.Response)
using (Stream stream = errorResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
string errorText = await reader.ReadToEndAsync();
Debug.WriteLine($"Error Response: {errorText}");
throw new Exception($"Telegram API error: {errorResponse.StatusCode} - {errorText}");
}
}
throw new Exception($"Network error: {ex.Message}");
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to send Telegram message: {ex.Message}");
throw;
}
}
}
}