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 Newtonsoft.Json;
using JsonNet.PrivateSettersContractResolvers; using JsonNet.PrivateSettersContractResolvers;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -12,10 +11,16 @@ namespace JetKarmaBot
public string ApiKey { get; private set; } public string ApiKey { get; private set; }
public string ConnectionString { get; private set; } public string ConnectionString { get; private set; }
public string ProxyUrl { get; private set; }
public int ProxyPort { get; private set; } public class ProxySettings
public string ProxyLogin { get; private set; } {
public string ProxyPassword { get; private set; } 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 public abstract class ConfigBase

View File

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

View File

@ -1,26 +1,61 @@
using System; using System;
using System.Threading;
using Perfusion; using Perfusion;
namespace JetKarmaBot 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(); Container c = new Container();
c.AddInstance(new Config("karma.cfg.json")); c.AddInstance(new Config("karma.cfg.json"));
Current = c.GetInstance(typeof(App)) as App; c.Add<Db>();
Console.ReadKey(); c.Add<JetKarmaBot>();
}
public static App Current { get; private set; } var bot = c.GetInstance<JetKarmaBot>();
[Inject(true)]
public void Run(Container c) try
{ {
Watcher = c.GetInstance<JetKarmaBot>(); bot.Init().Wait();
Console.WriteLine("JetKarmaBot started!"); 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