Finish moving to Perfusion

This commit is contained in:
jetsparrow 2018-12-19 22:42:53 +03:00
parent 128e0b3fd2
commit d200249650
4 changed files with 94 additions and 51 deletions

View File

@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using Newtonsoft.Json;
using JsonNet.PrivateSettersContractResolvers;
using Newtonsoft.Json.Linq;
@ -12,10 +11,16 @@ namespace JetKarmaBot
public string ApiKey { get; private set; }
public string ConnectionString { get; private set; }
public string ProxyUrl { get; private set; }
public int ProxyPort { get; private set; }
public string ProxyLogin { get; private set; }
public string ProxyPassword { get; private set; }
public class ProxySettings
{
public string Url { get; private set; }
public int Port { get; private set; }
public string Login { get; private set; }
public string Password { get; private set; }
}
public ProxySettings Proxy { get; private set; }
}
public abstract class ConfigBase

View File

@ -1,11 +1,7 @@
using JetKarmaBot.Commands;
using Perfusion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
@ -17,41 +13,38 @@ namespace JetKarmaBot
{
public class JetKarmaBot : IDisposable
{
public void Broadcast(string message)
{
foreach (var u in db.Chats)
client.SendTextMessageAsync(u.Value.ChatId, message);
}
[Inject(true)] Config Config { get; set; }
[Inject(true)] Container Container { get; set; }
[Inject(true)] Db Db { get; set; }
public JetKarmaBot([Inject(true)]Config cfg, [Inject(true)] Container container)
TelegramBotClient Client { get; set; }
ChatCommandRouter Commands;
User Me { get; set; }
public async Task Init()
{
var httpProxy = new WebProxy($"{cfg.ProxyUrl}:{cfg.ProxyPort}")
var httpProxy = new WebProxy($"{Config.Proxy.Url}:{Config.Proxy.Port}")
{
Credentials = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword)
Credentials = new NetworkCredential(Config.Proxy.Login, Config.Proxy.Password)
};
var botClient = new TelegramBotClient(cfg.ApiKey, httpProxy);
container.AddInstance(botClient);
var cred = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword);
client = new TelegramBotClient(cfg.ApiKey, httpProxy);
me = client.GetMeAsync().Result;
InitCommands(container);
client.OnMessage += BotOnMessageReceived;
client.StartReceiving();
Client = new TelegramBotClient(Config.ApiKey, httpProxy);
Container.AddInstance(Client);
Me = await Client.GetMeAsync();
InitCommands(Container);
Client.OnMessage += BotOnMessageReceived;
Client.StartReceiving();
}
#region IDisposable
public void Dispose()
public async Task Stop()
{
client.StopReceiving();
Dispose();
}
#endregion
#region service
[Inject(true)] Db db { get; set; }
TelegramBotClient client { get; }
User me { get; }
ChatCommandRouter commands;
void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
{
var message = messageEventArgs.Message;
@ -61,13 +54,23 @@ namespace JetKarmaBot
string s = message.Text;
long id = message.Chat.Id;
long from = message.From.Id;
Task.Run(() => commands.Execute(sender, messageEventArgs));
Task.Run(() => Commands.Execute(sender, messageEventArgs));
}
void InitCommands(Container c)
{
commands = new ChatCommandRouter();
commands.Add(c.ResolveObject(new StartCommand()));
commands.Add(c.ResolveObject(new AwardCommand(me)));
Commands.Add(c.ResolveObject(new StartCommand()));
Commands.Add(c.ResolveObject(new AwardCommand(Me)));
}
#endregion
#region IDisposable
public void Dispose()
{
Client.StopReceiving();
}
#endregion

View File

@ -1,26 +1,61 @@
using System;
using System.Threading;
using Perfusion;
namespace JetKarmaBot
{
public class App
public static class Program
{
public static void Main(string[] args)
public enum ExitCode : int
{
Ok = 0,
ErrorNotStarted = 0x80,
ErrorRunning = 0x81,
ErrorException = 0x82,
ErrorInvalidCommandLine = 0x100
};
#if DEBUG
public const bool Debug = true;
#else
public const bool Debug = false;
#endif
public static int Main(string[] args)
{
Container c = new Container();
c.AddInstance(new Config("karma.cfg.json"));
Current = c.GetInstance(typeof(App)) as App;
Console.ReadKey();
}
c.Add<Db>();
c.Add<JetKarmaBot>();
public static App Current { get; private set; }
[Inject(true)]
public void Run(Container c)
var bot = c.GetInstance<JetKarmaBot>();
try
{
Watcher = c.GetInstance<JetKarmaBot>();
Console.WriteLine("JetKarmaBot started!");
bot.Init().Wait();
Console.WriteLine("JetKarmaBot started. Press Ctrl-C to exit...");
Environment.ExitCode = (int)ExitCode.ErrorRunning;
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
Environment.ExitCode = (int)ExitCode.ErrorException;
}
ManualResetEvent quitEvent = new ManualResetEvent(false);
try
{
Console.CancelKeyPress += (sender, eArgs) => // ctrl-c
{
eArgs.Cancel = true;
quitEvent.Set();
};
}
catch { }
JetKarmaBot Watcher { get; set; }
quitEvent.WaitOne(Timeout.Infinite);
Console.WriteLine("Waiting for exit...");
bot?.Stop()?.Wait();
return (int)ExitCode.Ok;
}
}
}

@ -1 +1 @@
Subproject commit 470af231e3f7cf6942907d3b50473fa04dc04caf
Subproject commit 9a30df6d6e6573c901e4f792cbd08308ba1100a9