MajMcCloud / TelegramBotFramework

This is a context based application framework for the C# TelegramBot library.
https://www.t.me/tgbotbase
MIT License
149 stars 43 forks source link

Sample for asp.net core #36

Closed amirreza58 closed 1 year ago

amirreza58 commented 1 year ago

Hi Is it possible to provide an example for a asp.net core web application?

MajMcCloud commented 1 year ago

Hey!

you should be able to run it as a background task. I would say. Take a look at "HostedService"

Should be easy to implement.

Warm Regards Florian Zevedei

amirreza58 commented 1 year ago

Hi I tried using "HostedService" but it cause Errors

public class TelegramWorker : IHostedService
{

    private readonly IConfiguration _configuration;

    public TelegramWorker(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        var botConfig = _configuration.GetSection("TelegramBotConfiguration").Get<BotConfiguration>();

        var bot = BotBaseBuilder
                            .Create()
                            .WithAPIKey(botConfig.BotToken)
                            .DefaultMessageLoop()
                            .WithStartForm<StartForm>()
                            .WithHostAndPort("127.0.0.1", 10809)
                            .CustomCommands(a =>
                            {
                                a.Start("Starts the bot");

                            })
                            .NoSerialization()
                            .UseEnglish()
                            .Build();

        // Upload bot commands to BotFather
        //await bot.UploadBotCommands();

        // Start your Bot
        bot.Start();

    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}

first with "await bot.UploadBotCommands()" :

An error occurred while starting the application.
JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
Newtonsoft.Json.JsonTextReader.ParseValue()

RequestException: Required properties not found in response 1<!DOCTYPE html>
<html class="">
<head>
<meta charset="utf-8">
<title>Bots: An introduction for developers</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="description" content="Bots are small applications that run entirely within the Telegram app. Users interact with bots through flexible interfaces…">
<meta property="og:title" content="Bots: An introduction for developers">
<meta property="og:image" content="75f7c457f2561cbf84">
<meta property="og:description" content="Bots are small applications that run entirely within the Telegram app. Users interact with bots through flexible interfaces…">
<link rel="icon" type="image/svg+xml" href="/img/website_icon.svg?4">
<link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png">
<link rel="alternate icon" href="/img/favicon.ico" type="image/x-icon" />
<link href="/css/bootstrap.min.css?3" rel="stylesheet">

<link href="/css/telegram.css?233" rel="stylesheet" media="screen">
<style>
</style>
</head>
<body class="preload">
<div class="dev_page_wrap">
<div class="dev_page_head navbar navbar-static-top navbar-tg">
....

without it my app stopped with :

GET https://localhost:44314/ net::ERR_CONTENT_DECODING_FAILED 200

amirreza58 commented 1 year ago

ok i removed Telegram.Bot from .nuget and restored , problem solved.

MajMcCloud commented 1 year ago

Ok, so we can close this one ?