parser

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

 

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

Ответ

moko 22.09.2019 17:45

Сделать понятно можно. Начать вероятно стоит с изучения API Телеграм. У нас каркас одного из ботов выглядит примерно так:
@CLASS
Webhook

@OPTIONS
locals
static


@auto[]

$self.CURL_OPTIONS[
        $.connecttimeout(5)
        $.timeout(60)
        $.post(true)
        $.httpheader[
                $.[Content-Type][application/json]
        ]
]


@sendRequest[sUrl;sRequestBody]

^try{
        $f[^curl:load[$CURL_OPTIONS
                $.url[$sUrl]
                $.postfields[$sRequestBody]
        ]]
        $result[$f.text]
}{
        ^self.logError[]
        $exception.handled(true)
}

...


@CLASS
WebhookTelegram

@OPTIONS
locals
static

@BASE
Webhook


@auto[]

$self.TELEGRAM_API_URL[***]


@run[]

$response:status(200)

^switch[$env:REQUEST_METHOD]{
	^case[GET;POST]{
		^self.handlePost[]
	}
	^case[DEFAULT]{
		$response:status(403)
	}
}


@handlePost[]

^switch(true){
	^case(!(def $form:method)){
		^try{
			^if(!def $form:body){
				$hBody[^json:parse[^taint[clean][$request:body]]]
			}{
				$hBody[^json:parse[^taint[clean][$form:body]]]
			}
		}{
			$hBody[]
			$exception.handled(true)
		}
		^if(def $hBody){
			^self.handleUpdate[$hBody]
		}{
			$response:status(500)
		}
	}
	^case[DEFAULT]{
		^switch[$form:method]{
			^case[getMe;setWebhook]{
				^if($self.[$form:method] is junction){
					^self.[$form:method][]
				}
			}
			^case[DEFAULT]{
				$response:status(404)
			}
		}
	}
}


@handleUpdate[hBody]

^if(^hBody.update_id.double(0) > 0){
	^connect[$MAIN:SQL.connect-string]{
		$sReceivedMessage[^hBody.message.text.trim[]]
		^switch[$sReceivedMessage]{
			^case[/start]{
				$sPayload[GET_STARTED]
				$sText[Добрый день! Кого позвать?]
				$hReplyMarkup[
					$.keyboard[^table::create{Студию
Магазинус}]
					$.one_time_keyboard(true)
					$.resize_keyboard(true)
				]
			}
			^case[Студию]{
				$sPayload[GET_STUDIO]
				$sText[^self.getStudio[]]
			}
			^case[Магазинус]{
				$sPayload[GET_STORE]
				$sText[Отлично! Вот о чем я готов поболтать:]
				$hReplyMarkup[
					$.keyboard[^table::create{***}]
					$.one_time_keyboard(true)
					$.resize_keyboard(true)
				]
			}
			^case[DEFAULT]{
				$sText[^self.makeMessage[$hBody.message.from.id;^self.getPayload[$hBody.message.from.id];$sReceivedMessage]]
			}
		}

		^if(def $sPayload){
			^self.saveRequest[
				$.sender_id[$hBody.message.from.id]
				$.recipient_id[$hBody.message.chat.id]
				$.timestamp[$hBody.message.date]
				$.payload[$sPayload]
			]
		}

		$result[^self.apiResponse[sendMessage][
			$.chat_id[$hBody.message.chat.id]
			$.reply_to_message_id[$hBody.message.id]
			$.text[$sText]
			^if(def $hReplyMarkup){ $.reply_markup[$hReplyMarkup] }
		]]

		^if(!def $sPayload){
			^self.resetPayload[$hBody.message.from.id]
			^self.saveMessage[
				$.sender_id[$hBody.message.from.id]
				$.timestamp[$hBody.message.date]
				$.message[$sReceivedMessage]
			]
		}
	}
}


@getMe[]

$response:body[^self.apiResponse[getMe]]


@setWebhook[]

$response:body[^self.apiResponse[setWebhook][
	$.url[***]
]]


@apiResponse[sMethod;hResponse]

^if(!(def $sMethod)){
	^self.logError[Method name must be a string^#0A]
	^return[]
}

^if(!def $hResponse){
	$hResponse[^hash::create[]]
}(!($hResponse is hash)){
	^self.logError[hResponse must be an hash^#0A]
	^return[]
}

$result[^self.sendRequest[${TELEGRAM_API_URL}/$sMethod][^json:string[$hResponse][
	$.indent(false)
	$.one-line(true)
	$.table[array]
]]]