karmabot/JetKarmaBot/Services/Handling/RequestContext.cs
2019-12-09 08:53:19 +00:00

35 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JetKarmaBot.Commands;
using JetKarmaBot.Models;
using Telegram.Bot;
using Telegram.Bot.Args;
namespace JetKarmaBot.Services.Handling
{
public class RequestContext : IServiceProvider
{
public ITelegramBotClient Client { get; }
public MessageEventArgs EventArgs { get; }
public CommandString Command { get; }
public ICollection<object> Features { get; } = new List<object>();
public RequestContext(ITelegramBotClient client, MessageEventArgs args, CommandString cmd)
{
Client = client;
EventArgs = args;
Command = cmd;
}
public object GetService(Type serviceType) => Features.First(x => x.GetType() == serviceType);
public T GetFeature<T>() => (T)Features.First(x => x is T);
//Method to reduce WET in commands
public Task SendMessage(string text) => Client.SendTextMessageAsync(
EventArgs.Message.Chat.Id,
text,
replyToMessageId: EventArgs.Message.MessageId,
disableNotification: true,
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html);
}
}