Skip to content

Commit b77b191

Browse files
authored
Merge pull request #28 from GGrouppFoundation/feature/use-html-parse-mode-for-telegram
Use HTML parse mode in Telegram
2 parents 32cf04e + 2306f83 commit b77b191

File tree

7 files changed

+112
-53
lines changed

7 files changed

+112
-53
lines changed

src/Application/Application.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
<InvariantGlobalization>false</InvariantGlobalization>
99
<RootNamespace>GGroupp.Internal.Timesheet</RootNamespace>
1010
<AssemblyName>GGroupp.Internal.Timesheet.Bot.Application</AssemblyName>
11-
<Version>1.5.3</Version>
11+
<Version>1.5.4</Version>
1212
</PropertyGroup>
1313

1414
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Application' " />
1515

1616
<ItemGroup>
17-
<PackageReference Include="GGroupp.Infra.Bot.Builder.Authorization.Dataverse" Version="1.4.0" />
18-
<PackageReference Include="GGroupp.Infra.Bot.Builder.Command.Info" Version="1.2.1" />
19-
<PackageReference Include="GGroupp.Infra.Bot.Builder.Command.Menu" Version="1.1.1" />
20-
<PackageReference Include="GGroupp.Infra.Bot.Builder.Command.Stop" Version="1.2.1" />
17+
<PackageReference Include="GGroupp.Infra.Bot.Builder.Authorization.Dataverse" Version="1.4.1" />
18+
<PackageReference Include="GGroupp.Infra.Bot.Builder.Command.Info" Version="1.2.2" />
19+
<PackageReference Include="GGroupp.Infra.Bot.Builder.Command.Menu" Version="1.1.2" />
20+
<PackageReference Include="GGroupp.Infra.Bot.Builder.Command.Stop" Version="1.2.3" />
2121
<PackageReference Include="GGroupp.Infra.Bot.Builder.Integration.AspNet.Core" Version="1.5.0" />
2222
<PackageReference Include="GGroupp.Infra.Bot.Builder.Integration.CosmosDb" Version="1.1.0" />
2323
<PackageReference Include="GGroupp.Infra.Dataverse.Api" Version="2.8.0" />

src/DateTimesheet.Get/Step.DrawTimesheetSet/DrawActivity.cs

Lines changed: 99 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
using GGroupp.Infra.Bot.Builder;
33
using Microsoft.Bot.Builder;
44
using Microsoft.Bot.Schema;
5+
using System;
56
using System.Collections.Generic;
67
using System.Linq;
78
using System.Text;
9+
using System.Web;
810

911
namespace GGroupp.Internal.Timesheet;
1012

@@ -22,56 +24,27 @@ internal static IActivity CreateActivity(IChatFlowContext<DateTimesheetFlowState
2224
{
2325
if (context.FlowState.Timesheets?.Count is not > 0)
2426
{
25-
return MessageFactory.Text($"Нет списаний времени за {context.FlowState.Date.ToStringRussianCulture()}");
27+
return context.CreateTextActivity($"Нет списаний времени за {context.FlowState.Date.ToStringRussianCulture()}");
2628
}
2729

2830
if (context.IsCardSupported())
2931
{
3032
return CreateAdaptiveCardActivity(context);
3133
}
3234

33-
return CreateTextActivity(context);
34-
}
35-
36-
private static IActivity CreateTextActivity(IChatFlowContext<DateTimesheetFlowState> context)
37-
{
38-
var textBuilder = new StringBuilder(BuildHeader(context.FlowState));
39-
if (context.FlowState.Timesheets is null)
35+
if (context.IsTelegramChannel())
4036
{
41-
return MessageFactory.Text(textBuilder.ToString());
42-
}
43-
44-
foreach (var timesheetText in context.FlowState.Timesheets.Select(BuildTimesheetText))
45-
{
46-
textBuilder.AppendBotLine().Append(LineSeparator).AppendBotLine().Append(timesheetText);
47-
}
48-
49-
return MessageFactory.Text(textBuilder.ToString());
50-
51-
StringBuilder BuildTimesheetText(TimesheetJson timesheet)
52-
{
53-
var row = new StringBuilder().AppendFormat(
54-
"{0,-10}{1}",
55-
timesheet.Duration.ToDurationStringRussianCulture(true),
56-
context.EncodeTextWithStyle(timesheet.ProjectName, BotTextStyle.Bold));
57-
58-
var encodedDescription = context.EncodeTextWithStyle(timesheet.Description, BotTextStyle.Italic);
59-
if (string.IsNullOrEmpty(encodedDescription) is false)
60-
{
61-
row.AppendBotLine().Append(encodedDescription);
62-
}
63-
64-
return row;
37+
return context.Pipe(BuildTelegramText).Pipe(CreateTelegramTextActivity);
6538
}
6639

67-
static string BuildHeader(DateTimesheetFlowState flowState)
68-
=>
69-
string.Format(
70-
"{0,-10}**Всего {1}**",
71-
flowState.GetDurationSum().ToDurationStringRussianCulture(true),
72-
flowState.Date.ToStringRussianCulture());
40+
var text = BuildText(context);
41+
return MessageFactory.Text(text);
7342
}
7443

44+
private static IActivity CreateTextActivity(this ITurnContext turnContext, string text)
45+
=>
46+
turnContext.IsNotTelegramChannel() ? MessageFactory.Text(text) : CreateTelegramTextActivity(text);
47+
7548
private static IActivity CreateAdaptiveCardActivity(IChatFlowContext<DateTimesheetFlowState> context)
7649
=>
7750
new Attachment
@@ -84,6 +57,21 @@ private static IActivity CreateAdaptiveCardActivity(IChatFlowContext<DateTimeshe
8457
}
8558
.ToActivity();
8659

60+
private static IActivity CreateTelegramTextActivity(string text)
61+
{
62+
var channelData = new TelegramChannelData(
63+
parameters: new TelegramParameters(text)
64+
{
65+
ParseMode = TelegramParseMode.Html,
66+
ReplyMarkup = new TelegramReplyKeyboardRemove()
67+
});
68+
69+
var activity = MessageFactory.Text(default);
70+
activity.ChannelData = channelData.ToJObject();
71+
72+
return activity;
73+
}
74+
8775
private static List<AdaptiveElement> CreateAdaptiveBody(IChatFlowContext<DateTimesheetFlowState> context)
8876
{
8977
var adaptiveElements = new List<AdaptiveElement>
@@ -175,11 +163,81 @@ private static AdaptiveSchemaVersion GetAdaptiveSchemaVersion(this ITurnContext
175163
=>
176164
turnContext.IsMsteamsChannel() ? AdaptiveCard.KnownSchemaVersion : new(1, 0);
177165

178-
private static StringBuilder AppendBotLine(this StringBuilder builder)
166+
private static string BuildText(IChatFlowContext<DateTimesheetFlowState> context)
167+
{
168+
const string botLine = "\n\r\n\r";
169+
var flowState = context.FlowState;
170+
171+
var textBuilder = new StringBuilder().AppendRow(
172+
flowState.GetDurationSum().ToDurationStringRussianCulture(true), context.EncodeTextWithStyle(flowState.Date.ToStringRussianCulture(), BotTextStyle.Bold));
173+
174+
if (context.FlowState.Timesheets?.Count is not > 0)
175+
{
176+
return textBuilder.ToString();
177+
}
178+
179+
foreach (var timesheetText in context.FlowState.Timesheets.Select(BuildTimesheetText))
180+
{
181+
textBuilder.Append(botLine).Append(LineSeparator).Append(botLine).Append(timesheetText);
182+
}
183+
184+
return textBuilder.ToString();
185+
186+
StringBuilder BuildTimesheetText(TimesheetJson timesheet)
187+
{
188+
var row = new StringBuilder().AppendRow(
189+
timesheet.Duration.ToDurationStringRussianCulture(true), context.EncodeTextWithStyle(timesheet.ProjectName, BotTextStyle.Bold));
190+
191+
if (string.IsNullOrEmpty(timesheet.Description))
192+
{
193+
return row;
194+
}
195+
196+
return row.Append(botLine).AppendFormat(
197+
context.EncodeTextWithStyle(timesheet.Description, BotTextStyle.Italic));
198+
}
199+
}
200+
201+
private static string BuildTelegramText(IChatFlowContext<DateTimesheetFlowState> context)
202+
{
203+
const string botLine = "\n\r";
204+
var flowState = context.FlowState;
205+
206+
var textBuilder = new StringBuilder().AppendRow(
207+
flowState.GetDurationSum().ToDurationStringRussianCulture(true), $"<b>{flowState.Date.ToStringRussianCulture()}</b>");
208+
209+
if (context.FlowState.Timesheets?.Count is not > 0)
210+
{
211+
return textBuilder.ToString();
212+
}
213+
214+
foreach (var timesheetText in context.FlowState.Timesheets.Select(BuildTimesheetText))
215+
{
216+
textBuilder.Append(botLine).Append(LineSeparator).Append(botLine).Append(timesheetText);
217+
}
218+
219+
return textBuilder.ToString();
220+
221+
static StringBuilder BuildTimesheetText(TimesheetJson timesheet)
222+
{
223+
var row = new StringBuilder().AppendRow(
224+
timesheet.Duration.ToDurationStringRussianCulture(true), $"<b>{HttpUtility.HtmlEncode(timesheet.ProjectName)}</b>");
225+
226+
if (string.IsNullOrEmpty(timesheet.Description))
227+
{
228+
return row;
229+
}
230+
231+
return row.Append(botLine).Append(
232+
$"<i>{HttpUtility.HtmlEncode(timesheet.Description)}</i>");
233+
}
234+
}
235+
236+
private static StringBuilder AppendRow(this StringBuilder stringBuilder, string first, string second)
179237
=>
180-
builder.Append("\n\r");
238+
stringBuilder.AppendFormat("{0,-10}{1}", first, second);
181239

182240
private static decimal GetDurationSum(this DateTimesheetFlowState flowState)
183241
=>
184-
flowState.Timesheets?.Sum(x => x.Duration) ?? default;
242+
flowState.Timesheets?.Any() is true ? flowState.Timesheets.Sum(x => x.Duration) : default;
185243
}

src/Timesheet.Create/Step.FindProject/ProjectFindExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ internal static ValueTask<Result<LookupValueSetOption, BotFlowFailure>> SearchPr
5959
static @out => new LookupValueSetOption(
6060
items: @out.Projects.Select(MapProjectItem).ToArray(),
6161
choiceText: "Выберите проект"));
62+
6263
private static LookupValue MapFavorieProjectItem(FavoriteProjectItemGetOut item)
6364
=>
6465
new(item.Id, item.Name, item.Type.ToString("G"));

src/Timesheet.Create/Step.GetHourValue/HourValueGetFlowStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static Result<decimal, BotFlowFailure> ValidateValueOrFailure(decimal va
3636
value switch
3737
{
3838
not > 0 => BotFlowFailure.From("Значение должно быть больше нуля"),
39-
not <= MaxValue => BotFlowFailure.From(Invariant($"Значение должно быть меньше {MaxValue}")),
39+
not <= MaxValue => BotFlowFailure.From(Invariant($"Значение не может быть больше {MaxValue}")),
4040
_ => value
4141
};
4242

src/Timesheet.Create/Timesheet.Create.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Card" Version="1.2.1" />
15-
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Lookup" Version="2.3.1" />
16-
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Value" Version="2.3.1" />
14+
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Card" Version="1.2.3" />
15+
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Lookup" Version="2.3.3" />
16+
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Value" Version="2.3.3" />
1717
<PackageReference Include="GGroupp.Internal.Timesheet.FavoriteProjectSet.Get.InOut" Version="1.8.1" />
1818
<PackageReference Include="GGroupp.Internal.Timesheet.ProjectSet.Search.InOut" Version="1.3.1" />
1919
<PackageReference Include="GGroupp.Internal.Timesheet.Timesheet.Create.InOut" Version="1.4.2" />

src/Utility.Date.Get/TimesheetDateGetFlowStep.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private static DateStepOption CreateOptions<TFlowState>(IChatFlowContext<TFlowSt
3737
text: GetDateText(context),
3838
confirmButtonText: "Выбрать",
3939
invalidDateText: "Не удалось распознать дату",
40-
DateOnly.FromDateTime(DateTime.Now),
40+
defaultDate: DateOnly.FromDateTime(DateTime.Now),
4141
placeholder: DatePlaceholder,
4242
suggestions: context.CreateSuggestions(days));
4343

@@ -79,6 +79,6 @@ IDateSuggestionsRow CreateRow<TCollection>(TCollection days)
7979

8080
KeyValuePair<string, DateOnly> CreateSuggestion(DateOnly date)
8181
=>
82-
date == today ? new("Сегодня", date) : new(date.ToStringRussianCulture("dd.MM ddd"), date);
82+
date == today ? new("Сегодня", date) : new(date.ToStringRussianCulture("dd.MM ddd").ToUpperInvariant(), date);
8383
}
8484
}

src/Utility.Date.Get/Utility.Date.Get.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Date" Version="2.5.0" />
14+
<PackageReference Include="GGroupp.Infra.Bot.Builder.ChatFlow.Step.Date" Version="2.5.3" />
1515
</ItemGroup>
1616

1717
</Project>

0 commit comments

Comments
 (0)