2
2
using GGroupp . Infra . Bot . Builder ;
3
3
using Microsoft . Bot . Builder ;
4
4
using Microsoft . Bot . Schema ;
5
+ using System ;
5
6
using System . Collections . Generic ;
6
7
using System . Linq ;
7
8
using System . Text ;
9
+ using System . Web ;
8
10
9
11
namespace GGroupp . Internal . Timesheet ;
10
12
@@ -22,56 +24,27 @@ internal static IActivity CreateActivity(IChatFlowContext<DateTimesheetFlowState
22
24
{
23
25
if ( context . FlowState . Timesheets ? . Count is not > 0 )
24
26
{
25
- return MessageFactory . Text ( $ "Нет списаний времени за { context . FlowState . Date . ToStringRussianCulture ( ) } ") ;
27
+ return context . CreateTextActivity ( $ "Нет списаний времени за { context . FlowState . Date . ToStringRussianCulture ( ) } ") ;
26
28
}
27
29
28
30
if ( context . IsCardSupported ( ) )
29
31
{
30
32
return CreateAdaptiveCardActivity ( context ) ;
31
33
}
32
34
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 ( ) )
40
36
{
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 ) ;
65
38
}
66
39
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 ) ;
73
42
}
74
43
44
+ private static IActivity CreateTextActivity ( this ITurnContext turnContext , string text )
45
+ =>
46
+ turnContext . IsNotTelegramChannel ( ) ? MessageFactory . Text ( text ) : CreateTelegramTextActivity ( text ) ;
47
+
75
48
private static IActivity CreateAdaptiveCardActivity ( IChatFlowContext < DateTimesheetFlowState > context )
76
49
=>
77
50
new Attachment
@@ -84,6 +57,21 @@ private static IActivity CreateAdaptiveCardActivity(IChatFlowContext<DateTimeshe
84
57
}
85
58
. ToActivity ( ) ;
86
59
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
+
87
75
private static List < AdaptiveElement > CreateAdaptiveBody ( IChatFlowContext < DateTimesheetFlowState > context )
88
76
{
89
77
var adaptiveElements = new List < AdaptiveElement >
@@ -175,11 +163,81 @@ private static AdaptiveSchemaVersion GetAdaptiveSchemaVersion(this ITurnContext
175
163
=>
176
164
turnContext . IsMsteamsChannel ( ) ? AdaptiveCard . KnownSchemaVersion : new ( 1 , 0 ) ;
177
165
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 )
179
237
=>
180
- builder . Append ( " \n \r " ) ;
238
+ stringBuilder . AppendFormat ( "{0,-10}{1}" , first , second ) ;
181
239
182
240
private static decimal GetDurationSum ( this DateTimesheetFlowState flowState )
183
241
=>
184
- flowState . Timesheets ? . Sum ( x => x . Duration ) ?? default ;
242
+ flowState . Timesheets ? . Any ( ) is true ? flowState . Timesheets . Sum ( x => x . Duration ) : default ;
185
243
}
0 commit comments