1
1
using Telegram . Bot ;
2
2
using Telegram . Bot . Exceptions ;
3
+ using Telegram . Bot . Polling ;
3
4
using Telegram . Bot . Types ;
4
5
using Telegram . Bot . Types . Enums ;
5
6
using Telegram . Bot . Types . InlineQueryResults ;
6
7
using Telegram . Bot . Types . ReplyMarkups ;
7
8
8
9
namespace Webhook . Controllers . Services ;
9
10
10
- public class UpdateHandler
11
+ public class UpdateHandler ( ITelegramBotClient bot , ILogger < UpdateHandler > logger ) : IUpdateHandler
11
12
{
12
- private readonly TelegramBotClient _bot ;
13
- private readonly ILogger < UpdateHandler > _logger ;
14
13
private static readonly InputPollOption [ ] PollOptions = [ "Hello" , "World!" ] ;
15
14
16
- public UpdateHandler ( TelegramBotClient bot , ILogger < UpdateHandler > logger )
15
+ public async Task HandleErrorAsync ( ITelegramBotClient botClient , Exception exception , HandleErrorSource source , CancellationToken cancellationToken )
17
16
{
18
- _bot = bot ;
19
- _logger = logger ;
20
- }
21
-
22
- public async Task HandleErrorAsync ( Exception exception , CancellationToken cancellationToken )
23
- {
24
- _logger . LogInformation ( "HandleError: {exception}" , exception ) ;
17
+ logger . LogInformation ( "HandleError: {exception}" , exception ) ;
25
18
// Cooldown in case of network connection error
26
19
if ( exception is RequestException )
27
20
await Task . Delay ( TimeSpan . FromSeconds ( 2 ) ) ;
28
21
}
29
22
30
- public async Task HandleUpdateAsync ( Update update , CancellationToken cancellationToken )
23
+ public async Task HandleUpdateAsync ( ITelegramBotClient botClient , Update update , CancellationToken cancellationToken )
31
24
{
32
25
cancellationToken . ThrowIfCancellationRequested ( ) ;
33
26
await ( update switch
@@ -49,7 +42,7 @@ public async Task HandleUpdateAsync(Update update, CancellationToken cancellatio
49
42
50
43
private async Task OnMessage ( Message msg )
51
44
{
52
- _logger . LogInformation ( "Receive message type: {MessageType}" , msg . Type ) ;
45
+ logger . LogInformation ( "Receive message type: {MessageType}" , msg . Type ) ;
53
46
if ( msg . Text is not { } messageText )
54
47
return ;
55
48
@@ -66,7 +59,7 @@ private async Task OnMessage(Message msg)
66
59
"/throw" => FailingHandler ( msg ) ,
67
60
_ => Usage ( msg )
68
61
} ) ;
69
- _logger . LogInformation ( "The message was sent with id: {SentMessageId}" , sentMessage . MessageId ) ;
62
+ logger . LogInformation ( "The message was sent with id: {SentMessageId}" , sentMessage . MessageId ) ;
70
63
}
71
64
72
65
async Task < Message > Usage ( Message msg )
@@ -83,15 +76,15 @@ async Task<Message> Usage(Message msg)
83
76
/poll_anonymous - send an anonymous poll
84
77
/throw - what happens if handler fails
85
78
""" ;
86
- return await _bot . SendTextMessageAsync ( msg . Chat , usage , parseMode : ParseMode . Html , replyMarkup : new ReplyKeyboardRemove ( ) ) ;
79
+ return await bot . SendTextMessageAsync ( msg . Chat , usage , parseMode : ParseMode . Html , replyMarkup : new ReplyKeyboardRemove ( ) ) ;
87
80
}
88
81
89
82
async Task < Message > SendPhoto ( Message msg )
90
83
{
91
- await _bot . SendChatActionAsync ( msg . Chat , ChatAction . UploadPhoto ) ;
84
+ await bot . SendChatActionAsync ( msg . Chat , ChatAction . UploadPhoto ) ;
92
85
await Task . Delay ( 2000 ) ; // simulate a long task
93
86
await using var fileStream = new FileStream ( "Files/bot.gif" , FileMode . Open , FileAccess . Read ) ;
94
- return await _bot . SendPhotoAsync ( msg . Chat , fileStream , caption : "Read https://telegrambots.github.io/book/" ) ;
87
+ return await bot . SendPhotoAsync ( msg . Chat , fileStream , caption : "Read https://telegrambots.github.io/book/" ) ;
95
88
}
96
89
97
90
// Send inline keyboard. You can process responses in OnCallbackQuery handler
@@ -105,7 +98,7 @@ async Task<Message> SendInlineKeyboard(Message msg)
105
98
InlineKeyboardButton . WithUrl ( "WithUrl" , "https://github.com/TelegramBots/Telegram.Bot" )
106
99
] ,
107
100
] ;
108
- return await _bot . SendTextMessageAsync ( msg . Chat , "Inline buttons:" , replyMarkup : new InlineKeyboardMarkup ( buttons ) ) ;
101
+ return await bot . SendTextMessageAsync ( msg . Chat , "Inline buttons:" , replyMarkup : new InlineKeyboardMarkup ( buttons ) ) ;
109
102
}
110
103
111
104
async Task < Message > SendReplyKeyboard ( Message msg )
@@ -115,12 +108,12 @@ async Task<Message> SendReplyKeyboard(Message msg)
115
108
[ "1.1" , "1.2" , "1.3" ] ,
116
109
[ "2.1" , "2.2" ] ,
117
110
] ;
118
- return await _bot . SendTextMessageAsync ( msg . Chat , "Keyboard buttons:" , replyMarkup : new ReplyKeyboardMarkup ( keys ) { ResizeKeyboard = true } ) ;
111
+ return await bot . SendTextMessageAsync ( msg . Chat , "Keyboard buttons:" , replyMarkup : new ReplyKeyboardMarkup ( keys ) { ResizeKeyboard = true } ) ;
119
112
}
120
113
121
114
async Task < Message > RemoveKeyboard ( Message msg )
122
115
{
123
- return await _bot . SendTextMessageAsync ( msg . Chat , "Removing keyboard" , replyMarkup : new ReplyKeyboardRemove ( ) ) ;
116
+ return await bot . SendTextMessageAsync ( msg . Chat , "Removing keyboard" , replyMarkup : new ReplyKeyboardRemove ( ) ) ;
124
117
}
125
118
126
119
async Task < Message > RequestContactAndLocation ( Message msg )
@@ -130,24 +123,24 @@ async Task<Message> RequestContactAndLocation(Message msg)
130
123
KeyboardButton . WithRequestLocation ( "Location" ) ,
131
124
KeyboardButton . WithRequestContact ( "Contact" ) ,
132
125
] ;
133
- return await _bot . SendTextMessageAsync ( msg . Chat , "Who or Where are you?" , replyMarkup : new ReplyKeyboardMarkup ( buttons ) ) ;
126
+ return await bot . SendTextMessageAsync ( msg . Chat , "Who or Where are you?" , replyMarkup : new ReplyKeyboardMarkup ( buttons ) ) ;
134
127
}
135
128
136
129
async Task < Message > StartInlineQuery ( Message msg )
137
130
{
138
131
var button = InlineKeyboardButton . WithSwitchInlineQueryCurrentChat ( "Inline Mode" ) ;
139
- return await _bot . SendTextMessageAsync ( msg . Chat , "Press the button to start Inline Query\n \n " +
132
+ return await bot . SendTextMessageAsync ( msg . Chat , "Press the button to start Inline Query\n \n " +
140
133
"(Make sure you enabled Inline Mode in @BotFather)" , replyMarkup : new InlineKeyboardMarkup ( button ) ) ;
141
134
}
142
135
143
136
async Task < Message > SendPoll ( Message msg )
144
137
{
145
- return await _bot . SendPollAsync ( msg . Chat , "Question" , PollOptions , isAnonymous : false ) ;
138
+ return await bot . SendPollAsync ( msg . Chat , "Question" , PollOptions , isAnonymous : false ) ;
146
139
}
147
140
148
141
async Task < Message > SendAnonymousPoll ( Message msg )
149
142
{
150
- return await _bot . SendPollAsync ( chatId : msg . Chat , "Question" , PollOptions ) ;
143
+ return await bot . SendPollAsync ( chatId : msg . Chat , "Question" , PollOptions ) ;
151
144
}
152
145
153
146
static Task < Message > FailingHandler ( Message msg )
@@ -158,48 +151,48 @@ static Task<Message> FailingHandler(Message msg)
158
151
// Process Inline Keyboard callback data
159
152
private async Task OnCallbackQuery ( CallbackQuery callbackQuery )
160
153
{
161
- _logger . LogInformation ( "Received inline keyboard callback from: {CallbackQueryId}" , callbackQuery . Id ) ;
162
- await _bot . AnswerCallbackQueryAsync ( callbackQuery . Id , $ "Received { callbackQuery . Data } ") ;
163
- await _bot . SendTextMessageAsync ( callbackQuery . Message ! . Chat , $ "Received { callbackQuery . Data } ") ;
154
+ logger . LogInformation ( "Received inline keyboard callback from: {CallbackQueryId}" , callbackQuery . Id ) ;
155
+ await bot . AnswerCallbackQueryAsync ( callbackQuery . Id , $ "Received { callbackQuery . Data } ") ;
156
+ await bot . SendTextMessageAsync ( callbackQuery . Message ! . Chat , $ "Received { callbackQuery . Data } ") ;
164
157
}
165
158
166
159
#region Inline Mode
167
160
168
161
private async Task OnInlineQuery ( InlineQuery inlineQuery )
169
162
{
170
- _logger . LogInformation ( "Received inline query from: {InlineQueryFromId}" , inlineQuery . From . Id ) ;
163
+ logger . LogInformation ( "Received inline query from: {InlineQueryFromId}" , inlineQuery . From . Id ) ;
171
164
172
165
InlineQueryResult [ ] results = [ // displayed result
173
166
new InlineQueryResultArticle ( "1" , "Telegram.Bot" , new InputTextMessageContent ( "hello" ) ) ,
174
167
new InlineQueryResultArticle ( "2" , "is the best" , new InputTextMessageContent ( "world" ) )
175
168
] ;
176
- await _bot . AnswerInlineQueryAsync ( inlineQuery . Id , results , cacheTime : 0 , isPersonal : true ) ;
169
+ await bot . AnswerInlineQueryAsync ( inlineQuery . Id , results , cacheTime : 0 , isPersonal : true ) ;
177
170
}
178
171
179
172
private async Task OnChosenInlineResult ( ChosenInlineResult chosenInlineResult )
180
173
{
181
- _logger . LogInformation ( "Received inline result: {ChosenInlineResultId}" , chosenInlineResult . ResultId ) ;
182
- await _bot . SendTextMessageAsync ( chosenInlineResult . From . Id , $ "You chose result with Id: { chosenInlineResult . ResultId } ") ;
174
+ logger . LogInformation ( "Received inline result: {ChosenInlineResultId}" , chosenInlineResult . ResultId ) ;
175
+ await bot . SendTextMessageAsync ( chosenInlineResult . From . Id , $ "You chose result with Id: { chosenInlineResult . ResultId } ") ;
183
176
}
184
177
185
178
#endregion
186
179
187
180
private Task OnPoll ( Poll poll )
188
181
{
189
- _logger . LogInformation ( $ "Received Pull info: { poll . Question } ") ;
182
+ logger . LogInformation ( $ "Received Pull info: { poll . Question } ") ;
190
183
return Task . CompletedTask ;
191
184
}
192
185
193
186
private async Task OnPollAnswer ( PollAnswer pollAnswer )
194
187
{
195
188
var answer = pollAnswer . OptionIds . FirstOrDefault ( ) ;
196
189
var selectedOption = PollOptions [ answer ] ;
197
- await _bot . SendTextMessageAsync ( pollAnswer . User . Id , $ "You've chosen: { selectedOption . Text } in poll") ;
190
+ await bot . SendTextMessageAsync ( pollAnswer . User . Id , $ "You've chosen: { selectedOption . Text } in poll") ;
198
191
}
199
192
200
193
private Task UnknownUpdateHandlerAsync ( Update update )
201
194
{
202
- _logger . LogInformation ( "Unknown update type: {UpdateType}" , update . Type ) ;
195
+ logger . LogInformation ( "Unknown update type: {UpdateType}" , update . Type ) ;
203
196
return Task . CompletedTask ;
204
197
}
205
198
}
0 commit comments