TeleTok/Program.cs

108 lines
3.8 KiB
C#
Raw Normal View History

2023-02-13 16:34:22 -05:00
using Microsoft.Extensions.Configuration;
using System;
namespace TeleTok
{
2023-02-14 12:04:24 -05:00
public class TeleTok
2023-02-13 16:34:22 -05:00
{
2023-02-14 12:04:24 -05:00
public static IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", true)
.Build();
2023-02-13 16:34:22 -05:00
2023-02-16 10:53:08 -05:00
// Value to see what bot mode to run in
public static string botMode = config.GetSection("TeleTokConf:botMode").Value;
// Telegram bot config
2023-02-14 12:04:24 -05:00
public static string token = config.GetSection("TeleTokConf:token").Value;
2023-02-16 10:53:08 -05:00
public static string ptInstance = config.GetSection("TeleTokConf:ptInstance").Value;
// Matrix bot config
public static string matrixAddress = config.GetSection("TeleTokConf:matrixAddress").Value;
2023-02-16 17:03:20 -05:00
public static Uri mAddress;
2023-02-16 10:53:08 -05:00
public static string mBotUser = config.GetSection("TeleTokConf:mBotUser").Value;
public static string mBotPass = config.GetSection("TeleTokConf:mBotPass").Value;
2023-02-13 16:34:22 -05:00
2023-02-14 12:04:24 -05:00
static async Task Main(string[] args)
{
2023-02-16 10:53:08 -05:00
//Checks to see what mode to run the bot in
2023-02-16 17:03:20 -05:00
LogMessage($"The current running config is: \n Bot Mode: {botMode} \n Telegram Token: {token} \n ProxiTok Instance: {ptInstance} \n Matrix Homeserver: {matrixAddress} \n Matrix Bot User: {mBotUser}");
2023-02-16 10:53:08 -05:00
if(botMode == "telegram")
2023-02-15 13:53:47 -05:00
{
2023-02-16 10:53:08 -05:00
//Checks if the config json data is valid
2023-02-16 17:03:20 -05:00
if(!ConfigCheck(token))
2023-02-16 10:53:08 -05:00
{
LogMessage("Telegram bot token is invalid! Exiting...");
}
2023-02-16 17:03:20 -05:00
else if(!ConfigCheck(ptInstance))
2023-02-16 10:53:08 -05:00
{
LogMessage("Proxitok instance is invalid! Exiting...");
}
else
{
TelegramListener tListener = new TelegramListener();
LogMessage("Now listening...");
tListener.RunListener();
}
2023-02-15 13:53:47 -05:00
}
2023-02-16 10:53:08 -05:00
else if(botMode == "matrix")
2023-02-15 13:53:47 -05:00
{
2023-02-16 10:53:08 -05:00
//Checks if the config json data is valid
2023-02-16 17:03:20 -05:00
if(Uri.IsWellFormedUriString(matrixAddress, UriKind.Absolute))
2023-02-16 10:53:08 -05:00
{
2023-02-16 17:03:20 -05:00
mAddress = new Uri(matrixAddress);
2023-02-16 10:53:08 -05:00
}
2023-02-16 17:03:20 -05:00
else
{
LogMessage("Matrix server address is not a valid URL! Exiting...");
}
if(!ConfigCheck(mBotUser))
2023-02-16 10:53:08 -05:00
{
LogMessage("Matrix bot username is invalid! Exiting...");
}
2023-02-16 17:03:20 -05:00
else if(!ConfigCheck(mBotPass))
2023-02-16 10:53:08 -05:00
{
LogMessage("Matrix bot password is invalid! Exiting...");
}
else
{
MatrixListener mListener = new MatrixListener();
LogMessage("Now listening...");
2023-02-16 17:03:20 -05:00
await mListener.RunListener();
2023-02-16 10:53:08 -05:00
}
2023-02-15 13:53:47 -05:00
}
else
{
2023-02-16 10:53:08 -05:00
LogMessage("Bot mode is not configured! Enter either \'telegram\' or \'matrix\'");
}
}
2023-02-14 13:59:20 -05:00
2023-02-16 10:53:08 -05:00
public static bool ConfigCheck(string confItem)
{
if(confItem == "" || confItem == null
|| confItem == "INSERT TOKEN HERE"
|| confItem == "PROXITOK INSTANCE URL"
|| confItem == "SYNAPSE SERVER URL"
|| confItem == "MATRIX BOT USERNAMEL"
|| confItem == "MATRIX BOT PASSWORD")
{
return false;
}
else
{
return true;
}
2023-02-13 16:34:22 -05:00
}
2023-02-15 12:17:08 -05:00
public static void LogMessage(string text)
{
DateTime now =DateTime.Now;
Console.WriteLine("[" + now.ToString() + "] " + text);
}
2023-02-13 16:34:22 -05:00
}
}