parser

Написать ответ на текущее сообщение

 

 
   команды управления поиском

Ответ

ViRus 23.09.2019 11:59 / 23.09.2019 12:02

@CLASS
telegram

@BASE
MAIN

@OPTIONS
locals

################
@create[hParams]

# $hParams.token
# $hParams.curlLibPath

$telegram:token[$hParams.token]
^if(def $hParams.curlLibPath){
	$telegram:curlLibPath[$hParams.curlLibPath]
}($env:REMOTE_ADDR eq '127.0.0.1'){
	$telegram:curlLibPath[/usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0]
}
$telegram:apiBaseUrl[https://api.telegram.org/bot]

#############
@getUpdates[]

$result[^request[
	$.method[getUpdates]
]]

#####################
@sendMessage[hParams]

# $hParams.chatID
# $hParams.text

# $hParams.inlineKb[	^rem{ or replyKb }
#	$.row1[
#		$.btn1[
#			$.text[btn text]
#			$.callback_data[btn command]
#		]
#		...
#	]
#	...
# ]

^if($hParams.inlineKb){

	^hParams.inlineKb.foreach[;row]{

		^if(^inlineKbJson.right(1) eq ']'){
			$inlineKbJson[$inlineKbJson,]
		}
		$inlineKbJson[$inlineKbJson^[]
		^row.foreach[;btn]{

			^if(^inlineKbJson.right(1) eq '}'){
				$inlineKbJson[$inlineKbJson,]
			}
			$jsonBtn[^json:string[$btn]]
			$inlineKbJson[${inlineKbJson}$jsonBtn]

		}
		$inlineKbJson[$inlineKbJson^]]

	}

	$inlineKbJson[^text:removeSpaces[$inlineKbJson]]
	$inlineKbJson["inline_keyboard":[$inlineKbJson]]
	$reply_markup[$inlineKbJson]

}($hParams.replyKb){

	$reply_markup[^parseReplyKb[$hParams.replyKb]]

}{

#	$reply_markup[^parseReplyKb[^defaultReplyKb[]]]

}

$result[^request[
	$.method[sendMessage]
	$.hForm[
		$.chat_id[$hParams.chatID]
		$.text[^text:removeSpaces[$hParams.text]]
		$.parse_mode[HTML]
		$.reply_markup[{$reply_markup}]
	]
]]

#################
@request[hParams]

# $hParams.method
# $hParams.hForm

^if(
	def $curlLibPath
	&& !$telegram:isCurlLibLoaded
){

	^curl:options[
		$.library[$curlLibPath]
	]
	$telegram:isCurlLibLoaded(1)

}

^if($hParams.hForm){
	$formParams[]
	^hParams.hForm.foreach[k;v]{
		^if(def $formParams){
			$formParams[$formParams&]
		}
		$formParams[${formParams}$k=$v]
	}
}
$formParams[^taint[uri][$formParams]]
$curlRequest[^curl:load[
	$.url[^taint[as-is;${apiBaseUrl}$token/$hParams.method?$formParams]]
	$.ssl_verifypeer(0)
]]
$result[
	$.response[$curlRequest.text]
]

####################
@setWebHook[hParams]

# $hParams.url

$result[^request[
	$.method[setWebHook]
	$.hForm[
		$.url[$hParams.url]
	]
]]

######################
@sendLocation[hParams]

# $hParams.chatID
# $hParams.lat
# $hParams.lng

$result[^request[
	$.method[sendLocation]
	$.hForm[
		$.chat_id[$hParams.chatID]
		$.latitude[$hParams.lat]
		$.longitude[$hParams.lng]
	]
]]

##################
@parseReplyKb[hKb]

^hKb.foreach[;row]{

	^if(^result.right(1) eq ']'){
		$result[$result,]
	}
	$result[$result^[]
	^row.foreach[;btn]{

		^if(^result.right(1) eq '}'){
			$result[$result,]
		}
		$jsonBtn[^json:string[$btn]]
		$result[${result}$jsonBtn]

	}
	$result[$result^]]

}

$result[^text:removeSpaces[$result]]
$result["keyboard":[$result], "resize_keyboard": true]
Использование:
$telegram[^telegram::create[
	$.token[***]
	$.curlLibPath[***]
]]

$sendMsg[^telegram:sendMessage[
	$.chatID[***]
	$.text[***]
]]
прочее см. в коде.

Ответ в вебхуке ловится через $request:body. Либо без вебхука дергать getUpdates. Но это так себе.