@@ -11,6 +11,23 @@ import { checkAccess } from '../utils/checkAccess.js';
1111import ErrorHandler from './errorHandler.js' ;
1212
1313class OpenAIHandlers {
14+ sendResponse = ( ctx , sessions , sessionId ) => {
15+ return async ( response ) => {
16+ if ( response && response . content ) {
17+ sessions [ sessionId ] . messages . push ( {
18+ role : openai . roles . ASSISTANT ,
19+ content : response . content ,
20+ } ) ;
21+ }
22+
23+ await ctx . reply (
24+ response . content , { parse_mode : 'Markdown' } , menuKeyboard ,
25+ ) . catch (
26+ async ( ) => await ctx . reply ( response . content , menuKeyboard ) ,
27+ ) ;
28+ } ;
29+ } ;
30+
1431 textHandler = ( config , sessions ) => {
1532 return async ( ctx ) => {
1633 if ( await checkAccess ( config , ctx ) ) return ;
@@ -21,36 +38,19 @@ class OpenAIHandlers {
2138 const processing = await ctx . reply (
2239 code ( LEXICON_EN [ 'processingText' ] ) ,
2340 menuKeyboard ) ;
41+
2442 ctx . sendChatAction ( 'typing' ) ;
2543
26- const text = ctx . message . text ;
44+ const content = ctx . message . text ;
2745
2846 sessions [ sessionId ] . messages . push ( {
2947 role : openai . roles . USER ,
30- content : text ,
48+ content : content ,
3149 } ) ;
3250
3351 openai
3452 . chat ( sessions [ sessionId ] . messages )
35- . then ( async ( response ) => {
36- if ( response && response . content ) {
37- sessions [ sessionId ] . messages . push ( {
38- role : openai . roles . ASSISTANT ,
39- content : response . content ,
40- } ) ;
41- }
42-
43- await ctx
44- . reply (
45- response . content ,
46- { parse_mode : 'Markdown' } ,
47- menuKeyboard )
48- . catch ( async ( ) => {
49- await ctx . reply ( response . content ,
50- { parse_mode : 'HTML' } ,
51- ) ;
52- } ) ;
53- } ,
53+ . then ( this . sendResponse ( ctx , sessions , sessionId ) ,
5454 ) . catch ( ErrorHandler . responseError ( ctx , 'textHandler' ) ,
5555 ) . finally ( async ( ) => {
5656 await ctx . deleteMessage ( processing . message_id ) ;
@@ -68,6 +68,7 @@ class OpenAIHandlers {
6868 const processing = await ctx . reply (
6969 code ( LEXICON_EN [ 'processingVoice' ] ) ,
7070 menuKeyboard ) ;
71+
7172 ctx . sendChatAction ( 'typing' ) ;
7273
7374 const link = await ctx . telegram . getFileLink ( ctx . message . voice . file_id ) ;
@@ -86,23 +87,7 @@ class OpenAIHandlers {
8687
8788 openai
8889 . chat ( sessions [ sessionId ] . messages )
89- . then ( async ( response ) => {
90- sessions [ sessionId ] . messages . push ( {
91- role : openai . roles . ASSISTANT ,
92- content : response . content ,
93- } ) ;
94-
95- await ctx
96- . reply (
97- response . content ,
98- { parse_mode : 'Markdown' } ,
99- menuKeyboard )
100- . catch ( async ( ) => {
101- await ctx . reply ( response . content ,
102- { parse_mode : 'HTML' } ,
103- ) ;
104- } ) ;
105- } )
90+ . then ( this . sendResponse ( ctx , sessions , sessionId ) )
10691 . catch ( ErrorHandler . responseError ( ctx , 'transcription' ) ) ;
10792 } )
10893 . catch ( ErrorHandler . responseError ( ctx , 'voiceHandler' ) )
0 commit comments