Skip to content

Bale bot Api Wrapper for dotnet core

License

Notifications You must be signed in to change notification settings

MrAliSalehi/Bale.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NuGet stable version Nuget

Bale.Net

This is a Simple Bale bot Api Wrapper for dotnet core.

Install

you can get this package using nuget:

dotnet add package Bale.Net

or

NuGet\Install-Package Bale.Net

How To Use

first you have to instantiate the client and pass dawn your bot token(you can get it from @botfather):

var client = new BaleClient("your token");

after that you have access to 6 interfaces:

IAttachments, IChats, IMessages, IPayments, IUpdates, IUsers

just like the api, and you can just use all the methods like this:

//send message
var message = await _client.Messages.SendMessageAsync(MyId, Text);

//edit message
var message = await _client.Messages.EditMessageTextAsync(MyId, messageId, $"new txt");

//delete message
var message = await _client.Messages.DeleteMessageAsync(MyId, message.MessageId);

Note that in order to send any kind of Media(video, audio...etc) you need to work with Media Class, for example:

//send photo with url
var media = Media.FromUrl(new Uri("https://someImage.com/"));
var message = await _client.Attachments.SendPhotoAsync(chatId, media);

//send with file_id (the media already uploaded on the servers)
var media = Media.FromId("2939234i92fskkdofs");
var message = await _client.Attachments.SendPhotoAsync(chatId, media);

//upload from the disk

var media = Media.FromDisk("/home/Photo/somepic.png");
var message = await _client.Attachments.SendPhotoAsync(chatId, media);

other methods perform the same way, they all accept a Media, and you fill it the same way.

you can also read my tests to see the methods in action.

retry policy

this packages uses Polly to retry any failed API calls, you customize it's behaviour like the following:

  • set the delay between each attempt:
_client.Delay = TimeSpan.FromSeconds(1); //can be anything
  • set the maximum retry attempts (the default is 3):

    • to completely disable the retry policy you can set this property to zero.
_client.MaxRetryAttempts = 2;
  • get notified when a retry is happening:
_client.OnRetry = static arg =>
{
    Console.WriteLine(arg.AttemptNumber); //print the attempt counts
    return ValueTask.CompletedTask;
};

note that these settings should be done before any api calls, otherwise it wont have any effect. sample test.

TODO

  • implement retry policy for the http client
  • documentation for types
  • abstractions for update handling
  • more unit tests
  • SendContact throws internal error when reply_to_message_id is used

About

Bale bot Api Wrapper for dotnet core

Topics

Resources

License

Stars

Watchers

Forks

Languages