This is a .Net Standard client for communication with the Telegram Bot API. The client is available through a nuget.
Project url: https://appweb.se/en/packages/telegram-bot-client
You will find Telegram bot api documentation at:
Nuget package https://www.nuget.org/packages/AppWeb.TelegramBotClient/
install-package AppWeb.TelegramBotClient
- '/sendMessage' to send message to a chat by given chat_id
- '/getUpdates' get updates (messages) sent to the bot
- '/setWebhook' updates what webhook settings should be used.
- '/deleteWebhook' deletes your webhook configuration and you have to use /getUpdates instead
If '/setWebhook' is used you cannot receive updates through polling '/getUpdates' due to limitations in telegram bot api. You will also have to use a HTTPS connection for your webhook. Read more at: https://core.telegram.org/bots/api#setwebhook
- Implement more telegram bot api endpoints
- Nuget package
- Add tests
The provided project TelegramBotApiCMD provides you with tooling using the TelegramBotApiClient, using this you can test and configure your bot.
Available commands:
exit
to exit applicationgetupdates
to receive messageshelp
for helpdeletewebhook
to delete webhook configurationsetwebhook <webhookUrl>
to configure webhooksettoken <token>
to set token for current session
- One example of a .net core console application polling updates from bot api through '/getUpdates', it can also be used as a tool to configure your bot and read/send messages
- One example of a .net core web application setting webhook that receives updates and reply to the sender.
Example code for setting up webhook, see example project in solution
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// Setup telegram client
TelegramClient telegramClient = new TelegramClient();
var authenticationToken = Configuration["Settings:authenticationToken"];
telegramClient.SetAuthenticationToken(authenticationToken);
// Set up webhook
string webhookUrl = Configuration["Settings:webhookUrl"];
int maxConnections = int.Parse(Configuration["Settings:maxConnections"]);
UpdateType[] allowedUpdates = { UpdateType.MessageUpdate };
telegramClient.SetWebhook(webhookUrl, maxConnections, allowedUpdates);
services.AddScoped<ITelegramClient>(client => telegramClient);
}
Example code receiving webhook update and sending a replying message, see example project in solution
[Route("api/[controller]")]
public class TelegramController : Controller
{
ITelegramClient _telegramClient;
public TelegramController(ITelegramClient telegramClient) {
_telegramClient = telegramClient;
}
// GET api/telegram/update/{token}
[HttpPost("update/{token}")]
public void Update([FromRoute]string token, [FromBody]Update update)
{
if (token != _telegramClient.GetAuthenticationToken())
return;
if(update != null && update.Message != null)
_telegramClient.SendMessage(update.Message.Chat.Id, $"I received your message: \"{update.Message.Text}\"");
return;
}
}