Lparameters tcQuestion, lcChatId, tlMarkDown2Html, tcChatBot

* tlMarkDown2Html (optional, implicit .T.): converteste MarkDown in Html

Local loHTTP As 'MSXML2.ServerXMLHTTP'

Local lcChatId, lcQuestion, lcRaspuns, lcResponse, lcSampleReponse, lcURL, llSucces, loEx, loJson, llMarkDown2Html
LOCAL lcChatBot, lcChatBotId
lcRaspuns = ''

llMarkDown2Html = Iif(Pcount() <= 3, .T., m.tlMarkDown2Html)

tcQuestion = Strtran(m.tcQuestion, Chr(13), ' ',1,1000,1)
tcQuestion = Strtran(m.tcQuestion, '"', '\"',1,1000,1)

lcChatId = Iif(!Empty(m.lcChatId), m.lcChatId, '')

TEXT TO lcQuestion TEXTMERGE NOSHOW
{"question": "<<m.tcQuestion>>"<<IIF(!EMPTY(m.lcChatId), [, "chatId": "] + m.lcChatId + ["], [])>>
}
ENDTEXT

*!*	{"question": "Care este numele meu?",
*!*	"chatId": "913bf69d-7838-4f55-9c6d-d7ca8645be15"}

IF EMPTY(m.tcChatBot)
	lcChatBot = 'maria'
ELSE
	lcChatBot = ALLTRIM(LOWER(m.tcChatbot))
	IF !INLIST(LOWER(m.tcChatbot), 'maria', 'cezar')
		lcChatBot = 'maria'
	ENDIF 
ENDIF 

DO CASE 
	CASE m.lcChatBot = 'cezar'
		lcChatBotId = [462dabb9-6995-4f7e-ad8e-30624ae56be5]
	CASE m.lcChatBot = 'maria'
		lcChatBotId = [d4911620-07fe-41f8-adb4-f2f52d6ec766]
	OTHERWISE 		
		lcChatBotId = [462dabb9-6995-4f7e-ad8e-30624ae56be5]
ENDCASE 

lcURL = [https://mutual-special-koala.ngrok-free.app/api/v1/prediction/] + m.lcChatBotId

TRY 
	loHTTP = Createobject('MSXML2.ServerXMLHTTP')
	loHTTP.Open('POST', lcURL, .F.)
	loHTTP.setRequestHeader("Content-Type", "application/json; charset=utf-8")
	loHTTP.setRequestHeader("Accept-Charset", "UTF-8")

	loHTTP.Send(m.lcQuestion)
	llSucces = (loHTTP.Status = 200)
CATCH TO loEx
	llSucces = .F.
	AMESSAGEBOX(loEx.message,48,_Screen.Caption)
	lcRaspuns = loEx.message
ENDTRY 

If m.llSucces
	lcResponse = loHTTP.ResponseText
	Try
		loJson = nfjsonread(m.lcResponse)
	Catch To loEx
		llSucces = .F.	
		lcRaspuns = loEx.message
		AMESSAGEBOX(loEx.Message,48,_Screen.Caption)
	Endtry
	If Type('loJson.chatId') = 'C'
		lcChatId = loJson.chatId
	Endif

	If Type('loJson.text') = 'C'
		lcRaspuns = loJson.Text

		If m.llMarkDown2Html
			lcRaspuns = MarkdownToHtml(m.lcRaspuns)
		Endif
	Else
		lcRaspuns = m.lcResponse
	ENDIF
Endif

	TEXT TO lcSampleReponse
	{
	"chatId": "feec0271-7ab1-4f07-8953-3e3d9630a87e",
	"chatMessageId": "2c076f7c-3097-4d38-99d5-24ad2d319b85",
	"isStreamValid": true,
	"memoryType": "Buffer Window Memory",
	"question": "Care este procedura pentru borderoul efactura?",
	"sessionId": "feec0271-7ab1-4f07-8953-3e3d9630a87e",
	"text": "Procedura pentru Borderoul eFactura este urmatoarea:\n\n1.  Dupa emiterea facturilor, la sfarsitul zilei, sau a doua zi, daca se poate, in Borderoul eFactura: \n    *   Se citesc periodic Raspunsurile din ultimele 1-60 zile (depinde de cate zile nu s-au citit raspunsurile).\n    *   Facturile validate de ANAF au Tip Mesaj Raspuns = \"FACTURA PRIMITA\".\n    *   Facturile invalide au Tip Mesaj Raspuns = \"ERORI FACTURA\" ?i fundal de culoare ro?ie.\n    *   Se trimit facturile netrimise (culoare fundal galben pai) ?i cu erori, daca s-au corectat erorile, (culoare fundal rosu) din ziua curenta (daca operatia se face la sfarsitul zilei) sau din ziua precedenta (daca se poate).\n2.  Se citesc Raspunsurile ?i se retrimite eFactura doar daca s-a primit raspuns tip \"ERORI FACTURA\".\n3.  Din Borderoul eFactura, în paginile Facturi Trimise ?i Primite se poate lista centralizatorul facturilor trimise/primite în SPV cu Id Descarcare/Id Încarcare, pe care îl pute?i ata?a la Registrele de TVA Vânzare/Cumparare."
	}
	ENDTEXT

Return m.lcRaspuns

* Markdown to HTML Converter in Visual FoxPro
Function MarkdownToHtml
	Lparameters lcMarkdown

	Local lcHTML, laLines[1], lnI, lcLine, lcTemp
	lcHTML = ""

	* Split the Markdown into lines
	Alines(laLines, lcMarkdown)

	* Process each line
	For lnI = 1 To Alen(laLines)
		lcLine = Alltrim(laLines[lnI])

		* Check for headers
		Do Case
			Case Left(lcLine, 2) == '# '
				lcHTML = lcHTML + '<h1>' + Substr(lcLine, 3) + '</h1>' + Chr(13) + Chr(10)
			Case Left(lcLine, 3) == '## '
				lcHTML = lcHTML + '<h2>' + Substr(lcLine, 4) + '</h2>' + Chr(13) + Chr(10)
			Case Left(lcLine, 4) == '### '
				lcHTML = lcHTML + '<h3>' + Substr(lcLine, 5) + '</h3>' + Chr(13) + Chr(10)
			OTHERWISE 
				lcHtml = lcHtml + m.lcLine	
		Endcase
	Endfor

	* Check for bold and italic

	lcHTML = Strtran(m.lcHTML , '**', '<strong>',1,1000,1)
	lcHTML = Strtran(m.lcHTML , '**', '</strong>',1,1000,1)
	lcHTML = Strtran(m.lcHTML , '*', '<em>',1,1000,1)
	lcHTML = Strtran(m.lcHTML , '*', '</em>',1,1000,1)


	lcHTML = Strtran(m.lcHTML , '\n', '<br>',1,1000,1)
	lcHTML = Strtran(m.lcHTML , '\t', '&nbsp;&nbsp;&nbsp;&nbsp;',1,1000,1)
	lcHTML = Strtran(m.lcHTML , '\"', '&quot;',1,1000,1)

	Return m.lcHTML
Endfunc

