botman / driver-telegram

BotMan Telegram Driver
MIT License
86 stars 75 forks source link

[Feature] InlineQueryDriver - Inline Mode support #95

Closed vdomah closed 1 month ago

vdomah commented 3 years ago

Introducing new InlineQueryDriver.

Allows to handle Inline queries and send responses to show clickable lists in Telegram.

Use guide:

  1. Load driver:

    DriverManager::loadDriver(TelegramInlineQueryDriver::class);
  2. Add your listener as closure:

    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 <query>
    $query = $inline_query['query'];
    
    //get some records
    $models = Post::where('title', 'LIKE', '%' . $query . '%')
                    ->orWhere('text', 'LIKE', '%' . $query . '%')
                    ->limit(10)
                    ->get();
    
    //prepare them as InlineQueryResultArticle objects
    foreach ($models as $model) {
          $articles[] = new InlineQueryResultArticle([
                'id' => $model->id,
                'title' => $model->title,
                'description' => $model->text,
                'message_text' => $model->title . PHP_EOL . $model->text,
            ]);
        }
    
    $arArticlesJson = [];
    foreach ($arArticles as $article) {
        if ($article instanceof InlineQueryResultArticle)
            $arArticlesJson[] = $article->toJson();
    }
    
    return $arArticlesJson;
  3. Prevent fallback method from being triggered by inline_query:

    if (!isset($_POST['inline_query'])) { //add any of your logic into if statement
    $botman->fallback(...
    }
codecov[bot] commented 3 years ago

Codecov Report

Merging #95 into master will decrease coverage by 11.20%. The diff coverage is 7.77%.

Impacted file tree graph

@@              Coverage Diff              @@
##             master      #95       +/-   ##
=============================================
- Coverage     77.62%   66.41%   -11.21%     
- Complexity      133      156       +23     
=============================================
  Files            11       14        +3     
  Lines           429      536      +107     
=============================================
+ Hits            333      356       +23     
- Misses           96      180       +84     
Impacted Files Coverage Δ Complexity Δ
src/Extensions/Inline/InlineQueryResultArticle.php 0.00% <0.00%> (ø) 3.00 <3.00> (?)
src/TelegramContactDriver.php 0.00% <0.00%> (ø) 7.00 <7.00> (?)
src/TelegramInlineQueryDriver.php 0.00% <0.00%> (ø) 9.00 <9.00> (?)
src/TelegramDriver.php 92.45% <100.00%> (+0.21%) 64.00 <0.00> (+4.00)
src/Console/Commands/TelegramRegisterCommand.php 0.00% <0.00%> (ø) 6.00% <0.00%> (ø%)
src/TelegramAudioDriver.php 93.33% <0.00%> (+0.74%) 11.00% <0.00%> (ø%)
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 6c2f1df...d8e6784. Read the comment docs.