Skip to content

Commit f308e95

Browse files
committed
Sending photo via url, url buttons
1 parent c63f389 commit f308e95

9 files changed

Lines changed: 136 additions & 21 deletions

File tree

Deployf.Botf.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deployf.Botf.ChainedExample
2828
EndProject
2929
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{90A07D9F-B417-4D28-BC58-5D987CB90430}"
3030
EndProject
31-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Deployf.Botf.ActionButtonsExample", "Examples\Deployf.Botf.ActionButtonsExample\Deployf.Botf.ActionButtonsExample.csproj", "{7F105B52-AC24-416A-BAF5-12F5430BBCC2}"
31+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deployf.Botf.ActionButtonsExample", "Examples\Deployf.Botf.ActionButtonsExample\Deployf.Botf.ActionButtonsExample.csproj", "{7F105B52-AC24-416A-BAF5-12F5430BBCC2}"
32+
EndProject
33+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Deployf.Botf.MediaExample", "Examples\Deployf.Botf.MediaExample\Deployf.Botf.MediaExample.csproj", "{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}"
3234
EndProject
3335
Global
3436
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -72,6 +74,10 @@ Global
7274
{7F105B52-AC24-416A-BAF5-12F5430BBCC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
7375
{7F105B52-AC24-416A-BAF5-12F5430BBCC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
7476
{7F105B52-AC24-416A-BAF5-12F5430BBCC2}.Release|Any CPU.Build.0 = Release|Any CPU
77+
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
78+
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}.Debug|Any CPU.Build.0 = Debug|Any CPU
79+
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}.Release|Any CPU.ActiveCfg = Release|Any CPU
80+
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}.Release|Any CPU.Build.0 = Release|Any CPU
7581
EndGlobalSection
7682
GlobalSection(SolutionProperties) = preSolution
7783
HideSolutionNode = FALSE
@@ -85,6 +91,7 @@ Global
8591
{D3552144-80A0-4C3B-A0ED-A4A03691466B} = {90A07D9F-B417-4D28-BC58-5D987CB90430}
8692
{7E6BE982-6DCF-4CCB-AF7E-80CD5F90ED00} = {90A07D9F-B417-4D28-BC58-5D987CB90430}
8793
{7F105B52-AC24-416A-BAF5-12F5430BBCC2} = {90A07D9F-B417-4D28-BC58-5D987CB90430}
94+
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E} = {90A07D9F-B417-4D28-BC58-5D987CB90430}
8895
EndGlobalSection
8996
GlobalSection(ExtensibilityGlobals) = postSolution
9097
SolutionGuid = {558E8FF9-5AE8-4471-BF84-D79F5B0E91FB}

Deployf.Botf/BotController.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Linq.Expressions;
22
using Telegram.Bot;
33
using Telegram.Bot.Framework.Abstractions;
4+
using Telegram.Bot.Types;
45
using Telegram.Bot.Types.Enums;
56
using Telegram.Bot.Types.ReplyMarkups;
67

@@ -188,21 +189,36 @@ public async Task SendOrUpdate()
188189
protected async Task Send(string text, ParseMode mode)
189190
{
190191
IsDirty = false;
191-
var message = await Context!.Bot.Client.SendTextMessageAsync(
192-
Context!.GetSafeChatId()!,
193-
text,
194-
ParseMode.Html,
195-
replyMarkup: Message.Markup,
196-
cancellationToken: CancelToken,
197-
replyToMessageId: Message.ReplyToMessageId);
192+
Message message;
193+
if(Message.PhotoUrl == null)
194+
{
195+
message = await Client.SendTextMessageAsync(
196+
Context!.GetSafeChatId()!,
197+
text,
198+
ParseMode.Html,
199+
replyMarkup: Message.Markup,
200+
cancellationToken: CancelToken,
201+
replyToMessageId: Message.ReplyToMessageId);
202+
}
203+
else
204+
{
205+
message = await Client.SendPhotoAsync(
206+
Context!.GetSafeChatId()!,
207+
Message.PhotoUrl,
208+
text,
209+
ParseMode.Html,
210+
replyMarkup: Message.Markup,
211+
cancellationToken: CancelToken,
212+
replyToMessageId: Message.ReplyToMessageId);
213+
}
198214
await TryCleanLastMessageReplyKeyboard();
199215
await TrySaveLastMessageId(Message.Markup as InlineKeyboardMarkup, message);
200216
ClearMessage();
201217
}
202218

203219
public async Task UpdateMarkup(InlineKeyboardMarkup markup)
204220
{
205-
await Context!.Bot.Client.EditMessageReplyMarkupAsync(
221+
await Client.EditMessageReplyMarkupAsync(
206222
Context!.GetSafeChatId()!,
207223
Context!.GetSafeMessageId().GetValueOrDefault(),
208224
markup,
@@ -214,7 +230,7 @@ public async Task Update(InlineKeyboardMarkup? markup = null, string? text = nul
214230
{
215231
var markupValue = markup ?? Message.Markup as InlineKeyboardMarkup;
216232
IsDirty = false;
217-
var message = await Context!.Bot.Client.EditMessageTextAsync(
233+
var message = await Client.EditMessageTextAsync(
218234
Context!.GetSafeChatId()!,
219235
Context!.GetSafeMessageId().GetValueOrDefault(),
220236
text ?? Message.Message,
@@ -233,7 +249,7 @@ protected async Task Send(string text)
233249

234250
protected async Task AnswerCallback(string? text = null)
235251
{
236-
await Context!.Bot.Client.AnswerCallbackQueryAsync(Context!.GetCallbackQuery().Id,
252+
await Client.AnswerCallbackQueryAsync(Context!.GetCallbackQuery().Id,
237253
text,
238254
cancellationToken: CancelToken);
239255
}
@@ -386,6 +402,11 @@ public void Reply(int? messageId = default)
386402
}
387403
}
388404

405+
public void Photo(string url)
406+
{
407+
Message.SetPhotoUrl(url);
408+
}
409+
389410
public void ClearMessage()
390411
{
391412
Message = new MessageBuilder();

Deployf.Botf/Messages/MessageBuilder.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class MessageBuilder
99
public long ChatId { get; private set; }
1010
public StringBuilder BufferedMessage { get; private set; } = new StringBuilder();
1111
public IReplyMarkup? Markup { get; set; }
12+
public string PhotoUrl { get; set; }
1213
public List<List<InlineKeyboardButton>>? Reply { get; set; }
1314
public List<List<KeyboardButton>>? Keyboard { get; set; }
1415
public int ReplyToMessageId { get; set; } = 0;
@@ -36,6 +37,13 @@ public MessageBuilder SetMarkup(IReplyMarkup markup)
3637
return this;
3738
}
3839

40+
public MessageBuilder SetPhotoUrl(string url)
41+
{
42+
PhotoUrl = url;
43+
IsDirty = true;
44+
return this;
45+
}
46+
3947
public MessageBuilder PushL(string line = "")
4048
{
4149
Push(line + "\n");
@@ -90,7 +98,11 @@ public MessageBuilder RowButton(InlineKeyboardButton button)
9098

9199
public MessageBuilder Button(string text, string payload)
92100
{
93-
Button(InlineKeyboardButton.WithCallbackData(text, payload));
101+
var button = payload.IsUrl()
102+
? InlineKeyboardButton.WithUrl(text, payload)
103+
: InlineKeyboardButton.WithCallbackData(text, payload);
104+
105+
Button(button);
94106
return this;
95107
}
96108

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Telegram.Bot;
2+
using Telegram.Bot.Types;
23

34
namespace Deployf.Botf;
45

@@ -12,15 +13,30 @@ public MessageSender(ITelegramBotClient client)
1213
}
1314

1415
// TODO: to catch api exceptions about "forbidden"
15-
public async ValueTask Send(MessageBuilder message, CancellationToken token = default)
16+
public async ValueTask<Message> Send(MessageBuilder message, CancellationToken token = default)
1617
{
17-
await _client.SendTextMessageAsync(
18-
message.ChatId,
19-
message.Message,
20-
message.ParseMode,
21-
replyMarkup: message.Markup,
22-
cancellationToken: token,
23-
replyToMessageId: message.ReplyToMessageId
24-
);
18+
if (message.PhotoUrl == null)
19+
{
20+
return await _client.SendTextMessageAsync(
21+
message.ChatId,
22+
message.Message,
23+
message.ParseMode,
24+
replyMarkup: message.Markup,
25+
cancellationToken: token,
26+
replyToMessageId: message.ReplyToMessageId
27+
);
28+
}
29+
else
30+
{
31+
return await _client.SendPhotoAsync(
32+
message.ChatId,
33+
message.PhotoUrl,
34+
message.Message,
35+
message.ParseMode,
36+
replyMarkup: message.Markup,
37+
cancellationToken: token,
38+
replyToMessageId: message.ReplyToMessageId
39+
);
40+
}
2541
}
2642
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Deployf.Botf;
2+
3+
public static class StringExtensions
4+
{
5+
public static bool IsUrl(this string data)
6+
{
7+
return data.StartsWith("https://") || data.StartsWith("http://");
8+
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\Deployf.Botf\Deployf.Botf.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Deployf.Botf;
2+
3+
BotfProgram.StartBot(args);
4+
5+
class MediaController : BotController
6+
{
7+
[Action("/start")]
8+
void Start()
9+
{
10+
// Add the photo to message
11+
Photo("https://avatars.githubusercontent.com/u/59260433");
12+
Push("Hello from deploy-f");
13+
Button("Got to botf repo", "https://github.com/deploy-f/botf");
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"profiles": {
3+
"Deployf.Botf.MediaExample": {
4+
"commandName": "Project",
5+
"dotnetRunMessages": true,
6+
"launchBrowser": false,
7+
"applicationUrl": "http://localhost:5085",
8+
"environmentVariables": {
9+
"ASPNETCORE_ENVIRONMENT": "Development"
10+
}
11+
}
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)