botman / botman/driver-telegram

TelegramInlineQueryDriver [feature discussion]

Open
#94 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
94
Forks
74
PR merge metrics
No merged PRs in 30d

Description

I successfully implemented Inline Queries Driver in my project and I would like to make PR to this repo but I need some advices about how to do it properly to meet the package requirements.

So the steps are:
1. To create TelegramInlineQueryDriver class [DONE]
2. Write matching logic [DONE]
````
public function matchesRequest()
{
return ! is_null($this->payload->get('inline_query'));
}
````
3. Check that other driver will not match inline_query (TelegramDriver itself)

4. Here is the interesting part. Need to decide how TelegramInlineQueryDriver sends request to Telegram API.
Because inline queries don't use Conversations/Messages - we just need to send response directly.

5. In my project I've created buildServicePayloadInline instead of buildServicePayload method in TelegramInlineQueryDriver. buildServicePayloadInline will switch to answerInlineQuery endpoint:
````
$this->endpoint = 'answerInlineQuery';
````

6. There I was sending request with data needed:
````
$data = ['inline_query_id' => $inline_query_id, 'cache_time' => 0];
//forming $arArticlesJson array
$data['results'] = '[' . implode(',', $arArticlesJson) . ']';

return $data;
````
6. $arArticlesJson is array of InlineQueryResultArticle objects https://core.telegram.org/bots/api#inlinequeryresultarticle.

So now I'm wondering the most about "//forming $arArticlesJson array" part. We don't have Conversation classes here. But we need to inject our logic to prepare data that will be sent.

The most primitive way I see is for example to do that just by static method with closure:
````
DriverManager::loadDriver(TelegramInlineQueryDriver::class);

TelegramInlineQueryDriver::listen(function($inline_query) {
//developer got access to all inline query parameters:
//$inline_query['from']['id'];
//$inline_query['id'];
//$inline_query['query']; - the string user enters writing @bot_name

$data = ['inline_query_id' => $inline_query_id, 'cache_time' => 0];
//forming $arArticlesJson array
$data['results'] = '[' . implode(',', $arArticlesJson) . ']';

return $data;
});
````

What are your ideas on this, dear colleagues?

Contributor guide

Open the contributing guide

Research direction

Start with TelegramInlineQueryDriver, TelegramDriver, and DriverManager::loadDriver; review matchesRequest(), buildServicePayloadInline(), the answerInlineQuery endpoint, and the proposed listen callback. Done means agreeing on how inline-query results are prepared and sent while ensuring TelegramDriver does not match inline_query.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.