Skip to content

Files

Latest commit

 

History

History

Webhook.Controllers

ASP.NET Core Webhook Example

About

This project is a simple ASP.NET Core application, which provides webhook endpoint for the Telegram Bot.

You can find useful information on setting up webhook for your bot in official docs:

Setup

Please make sure you have .NET 8 or newer installed. You can download .NET runtime from the official site. This is a short description how you can test your bot locally. The description presumes that you already have a bot and it’s token. If not, please create one. You’ll find several explanations on the internet how to do this.

Bot configuration

You have to specify your Bot token in appsettings.json. Replace {BotToken} in appsettings.json with actual Bot token. Also you have to specify endpoint, to which Telegram will send new updates with HostAddress parameter:

"BotConfiguration": {
    "BotToken": "{BotToken}",
    "HostAddress": "https://mydomain.com"
}

you can specify separate development configuration with appsettings.Development.json.

Ngrok

Ngrok gives you the opportunity to access your local machine from a temporary subdomain provided by ngrok. This domain can later send to the telegram API as URL for the webhook. Install ngrok from this page: ngrok - download or via homebrew cask:

brew install --cask ngrok

and start ngrok for your localhost application url.

ngrok http --host-header=localhost https://localhost:44315

Telegram API only supports the ports 443, 80, 88 or 8443. Feel free to change the port in the config of the project.

Set Webhook

From ngrok you get an URL to your local server. It’s important to use the https one. You can manually set webhook with setWebhook API call, providing this URL as form-data (key: url, value: https://yoursubdomain.ngrok.io/api/update).

Run Bot

Now you can start the Bot in a local instance. Check if the port of the application matches the port on which ngrok is running.

Now your bot should answer with the text from every message you send to it.