Giter VIP home page Giter VIP logo

telegram.bot.framework's Introduction

Telegram Bot Framework for .NET Core

NuGet Build Status License

Telegram Bot Framework Logo

Simple framework for building Telegram bots ๐Ÿค–. Ideal for running multiple chat bots inside a single ASP.NET Core app.

See some sample bots in action:

Getting Started

This project targets .NET Standard 1.6 so make sure you have Visual Studio 2017 or .NET Core (v1.1 or above) installed.

Creating a bot with good architecture becomes very simple using this framework. Have a look at the Quick Start wiki to make your fist Echo Bot.

There is much more you can do with your bot. See what's available at wikis.

Framework Features

  • Allows you to have multiple bots running inside one app
  • Able to share code(update handlers) between multiple bots
  • Easy to use with webhooks(specially with Docker deployments)
  • Optimized for making Telegram Games
  • Simplifies many repititive tasks in developing bots

Samples

Don't wanna read wikis? Read C# code of sample projects in samples directory.

telegram.bot.framework's People

Contributors

jharrilim avatar justdmitry avatar poulad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

telegram.bot.framework's Issues

Cancellation token

First cancellation token passed to the update delegate should be None for the polling update and request's timeout for the webhooks.

Shared scoped between different updates

In your current implementation all update handlers share a scope because getUpdates can return more than one update at a time. I think it would be better if every individual update would have it's own scope. It can be achieved by using IServiceScopeFactory and explicitly creating scope for each update inside HandleUpdateAsync.

Introduce shared mutable context between update handlers

What do you think of the idea making update handlers more like middlewares is ASP.NET Core which share mutable context instance? That way there could be a common session middleware (e.g. with separate storages for a chat and a user). Or a middleware that constructs ClaimsPrincipal and attaches it to the context instance. It would open possibilities to leverage existing packages from ASP.NET Core that aren't tied to HttpContext or HTTP in general like Authorization.

Bug in the UseWhen rejoining branch

The UseWhen method doesn't wire up the next delegate properly.

Expected

In the example pipeline below, the C middleware should be the next delegate passed to the B middleware. Therefore, C should be invoked only if next is invoked in the B.

new BotBuilder()
  .Use<A>()
  .UseWhen(predicate, branch =>
    branch.Use<B>()
  )
  .Use<C>()

Actual

But the C middleware is always invoked regardless of calling the next delegate in B.

Refactoring the `CommandBase`

Methods of a command class (canHandle, parse, handle) should only get a Message as input not the Update because it could be confusing to the user.

Also, updates of types other than [Edited]MessageUpdate are never considered as command.

btw, CommandBase.CanHandleCommand doesn't need to be virtual.

Redesign IUpdateManager interface and implementation

Scoped lifetime of IUpdateManager<TBot> and polling functionality built in it don't play well together. An entity that performs polling should be a singleton.

IUpdateManager<TBot> functionality should be split to two interfaces:

  • IUpdateDispatcher<TBot> that performs dispatching and could has either scoped or transient lifetime
  • An entity that performs actual polling and has dependency on a factory that creates IUpdateDisptacher<TBot> instance and has singleton lifetime so it correctly incapsulates update offset

Strongly typed updates

It would be nicer to handle updates of a specific type.

Cleaner code:

class MyClass {
    void RespondMessageWithWelcome(Update<MessageUpdate> msgUpdate) { }
    void ReplyToCallbackquery(Update<CallbackQueryUpdate> queryUpdate) { }
}

Possible implementation:

Update Types:

abstract class UpdateContentBase { }
class MessageUpdate : UpdateContentBase { }
class CallbackQueryUpdate : UpdateContentBase { }

New Update class:

interface IUpdate<out T> where T : UpdateContentBase, new() {
    T Content { get; }
}

class Update<T> : IUpdate<T> where T : UpdateContentBase, new() {
    public T Content { get; }
    public Update(T content) {
        Content = content;
    }
}

Not really sure if this is going to work.

Telegram.Bot vs Telegram.Bot.Framework

Hi!

I see there are two similar nuget packages created by you, both for Telegram Bot. They both can target asp.net core, thus it's not clear how are they different and when to use each of those.

Could you clarify on that?

Probably a problem with long polling

Default timeout in GetUpdateAsync method is set to 0. BotManager should take timeout from ITelegramBotClient and pass it to GetUpdatesAsync method. I think there also should be a config for timeout in BotOptions.

BotManager in webhook mode

There is a problem with IBotManager<TBot> instances lifetime in webhook mode. Each middleware in ASP.NET Core is instantiated once and is kept in memory as a part of middleware pipeline. So it is essentially a singleton. But IBotManager<TBot> has scoped lifetime. IBotManager<TBot> should be injected as a parameter of Invoke method here.

UseCommand

Perhaps UseCommand() should not be a terminating middleware like Map.

SampleGames how to send score?

How should I send score to a game backend? It's not well described in the documentation.
Could you please help me with that?

ConfigurationException in development mode on ASP.NET Core 2.0

When used with ASP.NET Core 2.0 and ASPNETCORE_ENVIRONMENT is set to Development and long polling mode is used UseTelegramBotLongPolling throws this exception.

Creating explicit scope inside extension methods (UseTelegramBotWebhook, etc) and passing scoped ServiceProvider to FindBotManager helps but I don't understand why. When used with webhook everything works fine and no exception is thrown.

SampleEchoBot is not working

nGetting build error for sample echo bot project. Tried referring the Telegram Framework project and from
sampleechobot
Nuget.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.