Skip to content

Commit

Permalink
Sending photo via url, url buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
k0dep committed Apr 9, 2022
1 parent c63f389 commit f308e95
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 21 deletions.
9 changes: 8 additions & 1 deletion Deployf.Botf.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deployf.Botf.ChainedExample
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{90A07D9F-B417-4D28-BC58-5D987CB90430}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Deployf.Botf.ActionButtonsExample", "Examples\Deployf.Botf.ActionButtonsExample\Deployf.Botf.ActionButtonsExample.csproj", "{7F105B52-AC24-416A-BAF5-12F5430BBCC2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deployf.Botf.ActionButtonsExample", "Examples\Deployf.Botf.ActionButtonsExample\Deployf.Botf.ActionButtonsExample.csproj", "{7F105B52-AC24-416A-BAF5-12F5430BBCC2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Deployf.Botf.MediaExample", "Examples\Deployf.Botf.MediaExample\Deployf.Botf.MediaExample.csproj", "{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -72,6 +74,10 @@ Global
{7F105B52-AC24-416A-BAF5-12F5430BBCC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F105B52-AC24-416A-BAF5-12F5430BBCC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F105B52-AC24-416A-BAF5-12F5430BBCC2}.Release|Any CPU.Build.0 = Release|Any CPU
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -85,6 +91,7 @@ Global
{D3552144-80A0-4C3B-A0ED-A4A03691466B} = {90A07D9F-B417-4D28-BC58-5D987CB90430}
{7E6BE982-6DCF-4CCB-AF7E-80CD5F90ED00} = {90A07D9F-B417-4D28-BC58-5D987CB90430}
{7F105B52-AC24-416A-BAF5-12F5430BBCC2} = {90A07D9F-B417-4D28-BC58-5D987CB90430}
{F3B7EFA5-02B0-4970-9FAA-E4DD7D44C45E} = {90A07D9F-B417-4D28-BC58-5D987CB90430}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {558E8FF9-5AE8-4471-BF84-D79F5B0E91FB}
Expand Down
41 changes: 31 additions & 10 deletions Deployf.Botf/BotController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq.Expressions;
using Telegram.Bot;
using Telegram.Bot.Framework.Abstractions;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;

Expand Down Expand Up @@ -188,21 +189,36 @@ public async Task SendOrUpdate()
protected async Task Send(string text, ParseMode mode)
{
IsDirty = false;
var message = await Context!.Bot.Client.SendTextMessageAsync(
Context!.GetSafeChatId()!,
text,
ParseMode.Html,
replyMarkup: Message.Markup,
cancellationToken: CancelToken,
replyToMessageId: Message.ReplyToMessageId);
Message message;
if(Message.PhotoUrl == null)
{
message = await Client.SendTextMessageAsync(
Context!.GetSafeChatId()!,
text,
ParseMode.Html,
replyMarkup: Message.Markup,
cancellationToken: CancelToken,
replyToMessageId: Message.ReplyToMessageId);
}
else
{
message = await Client.SendPhotoAsync(
Context!.GetSafeChatId()!,
Message.PhotoUrl,
text,
ParseMode.Html,
replyMarkup: Message.Markup,
cancellationToken: CancelToken,
replyToMessageId: Message.ReplyToMessageId);
}
await TryCleanLastMessageReplyKeyboard();
await TrySaveLastMessageId(Message.Markup as InlineKeyboardMarkup, message);
ClearMessage();
}

public async Task UpdateMarkup(InlineKeyboardMarkup markup)
{
await Context!.Bot.Client.EditMessageReplyMarkupAsync(
await Client.EditMessageReplyMarkupAsync(
Context!.GetSafeChatId()!,
Context!.GetSafeMessageId().GetValueOrDefault(),
markup,
Expand All @@ -214,7 +230,7 @@ public async Task Update(InlineKeyboardMarkup? markup = null, string? text = nul
{
var markupValue = markup ?? Message.Markup as InlineKeyboardMarkup;
IsDirty = false;
var message = await Context!.Bot.Client.EditMessageTextAsync(
var message = await Client.EditMessageTextAsync(
Context!.GetSafeChatId()!,
Context!.GetSafeMessageId().GetValueOrDefault(),
text ?? Message.Message,
Expand All @@ -233,7 +249,7 @@ protected async Task Send(string text)

protected async Task AnswerCallback(string? text = null)
{
await Context!.Bot.Client.AnswerCallbackQueryAsync(Context!.GetCallbackQuery().Id,
await Client.AnswerCallbackQueryAsync(Context!.GetCallbackQuery().Id,
text,
cancellationToken: CancelToken);
}
Expand Down Expand Up @@ -386,6 +402,11 @@ public void Reply(int? messageId = default)
}
}

public void Photo(string url)
{
Message.SetPhotoUrl(url);
}

public void ClearMessage()
{
Message = new MessageBuilder();
Expand Down
14 changes: 13 additions & 1 deletion Deployf.Botf/Messages/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class MessageBuilder
public long ChatId { get; private set; }
public StringBuilder BufferedMessage { get; private set; } = new StringBuilder();
public IReplyMarkup? Markup { get; set; }
public string PhotoUrl { get; set; }
public List<List<InlineKeyboardButton>>? Reply { get; set; }
public List<List<KeyboardButton>>? Keyboard { get; set; }
public int ReplyToMessageId { get; set; } = 0;
Expand Down Expand Up @@ -36,6 +37,13 @@ public MessageBuilder SetMarkup(IReplyMarkup markup)
return this;
}

public MessageBuilder SetPhotoUrl(string url)
{
PhotoUrl = url;
IsDirty = true;
return this;
}

public MessageBuilder PushL(string line = "")
{
Push(line + "\n");
Expand Down Expand Up @@ -90,7 +98,11 @@ public MessageBuilder RowButton(InlineKeyboardButton button)

public MessageBuilder Button(string text, string payload)
{
Button(InlineKeyboardButton.WithCallbackData(text, payload));
var button = payload.IsUrl()
? InlineKeyboardButton.WithUrl(text, payload)
: InlineKeyboardButton.WithCallbackData(text, payload);

Button(button);
return this;
}

Expand Down
34 changes: 25 additions & 9 deletions Deployf.Botf/Messages/MessageSender.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Telegram.Bot;
using Telegram.Bot.Types;

namespace Deployf.Botf;

Expand All @@ -12,15 +13,30 @@ public MessageSender(ITelegramBotClient client)
}

// TODO: to catch api exceptions about "forbidden"
public async ValueTask Send(MessageBuilder message, CancellationToken token = default)
public async ValueTask<Message> Send(MessageBuilder message, CancellationToken token = default)
{
await _client.SendTextMessageAsync(
message.ChatId,
message.Message,
message.ParseMode,
replyMarkup: message.Markup,
cancellationToken: token,
replyToMessageId: message.ReplyToMessageId
);
if (message.PhotoUrl == null)
{
return await _client.SendTextMessageAsync(
message.ChatId,
message.Message,
message.ParseMode,
replyMarkup: message.Markup,
cancellationToken: token,
replyToMessageId: message.ReplyToMessageId
);
}
else
{
return await _client.SendPhotoAsync(
message.ChatId,
message.PhotoUrl,
message.Message,
message.ParseMode,
replyMarkup: message.Markup,
cancellationToken: token,
replyToMessageId: message.ReplyToMessageId
);
}
}
}
9 changes: 9 additions & 0 deletions Deployf.Botf/System/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Deployf.Botf;

public static class StringExtensions
{
public static bool IsUrl(this string data)
{
return data.StartsWith("https://") || data.StartsWith("http://");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Deployf.Botf\Deployf.Botf.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions Examples/Deployf.Botf.MediaExample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Deployf.Botf;

BotfProgram.StartBot(args);

class MediaController : BotController
{
[Action("/start")]
void Start()
{
// Add the photo to message
Photo("https://avatars.githubusercontent.com/u/59260433");
Push("Hello from deploy-f");
Button("Got to botf repo", "https://github.com/deploy-f/botf");
}
}
13 changes: 13 additions & 0 deletions Examples/Deployf.Botf.MediaExample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"Deployf.Botf.MediaExample": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5085",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
9 changes: 9 additions & 0 deletions Examples/Deployf.Botf.MediaExample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit f308e95

Please sign in to comment.