Skip to content

Commit

Permalink
Fix update types error, Add inline queries test
Browse files Browse the repository at this point in the history
  • Loading branch information
Muaath5 committed Dec 10, 2021
1 parent 3193b94 commit 53bf4bc
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 41 deletions.
79 changes: 40 additions & 39 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file removed examples/InlineQueriesBot.php
Empty file.
90 changes: 90 additions & 0 deletions examples/InlineQueriesIndexBot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
# This file should be a webhook on your website

namespace SimpleBotAPI\Examples;

# Reuire autoload.php
//require __DIR__ . '/vendor/autoload.php';

use SimpleBotAPI\BotSettings;
use SimpleBotAPI\TelegramBot;
use SimpleBotAPI\UpdatesHandler;

# The webhook url should be:
# https://mywebsite.com/InlineQueriesBot.php?token={bot_token}

# Check authentication, Or maybe using library check authentication
if (isset($_REQUEST['token']))
{
if ($_REQUEST['token'] == getenv('BOT_TOKEN'))
{
$Bot = new TelegramBot(getenv('BOT_TOKEN'), new InlineQueriesIndexBot([
'Secret' => 'cce23ef_Qa98HIDUROV256bI0t',
'Telegram' => 'Telegram is a partially open source messaging app started in 2013 by @Durov..'
]), new BotSettings());

# Process Webhook Update
$Bot->OnWebhookUpdate();
}
}

class InlineQueriesIndexBot extends UpdatesHandler
{
public array $Index;
public function __construct(array $index) {
$this->Index = $index;
}

# Write the handler for updates that your bot needs
public function MessageHandler(object $message): bool
{
if (property_exists($message, 'via_bot'))
{
if ($message->via_bot->username == 'Muaath_5_Test_Bot')
return true;
}
$this->Bot->SendMessage([
'chat_id' => $message->chat->id,
'text' => 'Type any number, And bot will show its answers',
'reply_markup' => json_encode([
'inline_keyboard' => [
[
[
'text' => 'Try an inline query',
'switch_inline_query_current_chat' => '-1'
]
]
]
])
]);
return true;
}

public function InlineQueryHandler(object $inline_query): bool
{
$result = [];
if (isset($this->Index[$inline_query->query]))
{
$result = [
[
'type' => 'article',
'id' => $inline_query->query,
'title' => "Answer of {$inline_query->query} query",
'description' => 'Click to get the answer',
'input_message_content' => [
'message_text' => $this->Index[$inline_query->query],
'parse_mode' => 'Markdown'
]
]
];
}
$this->Bot->AnswerInlineQuery([
'inline_query_id' => $inline_query->id,
'cache_time' => 1000,
'results' => json_encode($result),
'switch_pm_text' => 'How to use the bot?',
'switch_pm_parameter' => 'howtouse'
]);
return true;
}
}
2 changes: 1 addition & 1 deletion test/ContactMeBotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testRun() : void
}
$Bot->SendMessage([
'chat_id' => 1265170068,
'text' => 'ContactMeBotTest.php was stopped..'
'text' => 'ContactMeBotTest.php stopped.'
]);

$this->assertEquals(true, true);
Expand Down
2 changes: 1 addition & 1 deletion test/FAQBotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testRun() : void

$Bot->SendMessage([
'chat_id' => 1265170068,
'text' => 'FAQBot.php was stopped..'
'text' => 'FAQBot.php stopped.'
]);

$this->assertEquals(true, true);
Expand Down
47 changes: 47 additions & 0 deletions test/InlineQueriesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

require dirname(__DIR__) . '/vendor/autoload.php';

use PHPUnit\Framework\TestCase;
use SimpleBotAPI\TelegramBot;
use SimpleBotAPI\Examples\InlineQueriesIndexBot;

final class InlineQueriesTest extends TestCase
{
public function testInlineQueries() : void
{
$this->assertNotFalse(getenv('TEST_BOT_TOKEN'), 'TEST_BOT_TOKEN Environment variable is empty');
$Bot = new TelegramBot(getenv('TEST_BOT_TOKEN'), new InlineQueriesIndexBot([
'-1' => 'ERROR [400]: Invalid query ID',
'1' => "Secret!!",
'2' => "How did you discovered this?",
'200' => 'SUCCESS [200]: Success!',
'303' => 'REDIRECT [303]: Redirect',
'404' => 'ERROR [404]: Not found',
'406' => 'ERROR [406]: Update app',
'509' => 'Author: Muaath **Alqarni**',
'2013' => 'This was the year of creating Telegram!',
'2019' => 'TAKE CARE :: COVID-19 HERE',
'2021' => 'This is the year of creating @DIBgram!'
]));

$Bot->SendMessage([
'chat_id' => 1265170068,
'text' => 'InlineQueriesTest.php started!'
]);

$stop_time = strtotime('+3 minutes');
while (time() < $stop_time)
{
$Bot->ReceiveUpdates();
}

$Bot->SendMessage([
'chat_id' => 1265170068,
'text' => 'InlineQueriesTest.php stopped.'
]);

$this->assertEquals(true, true);
}
}

0 comments on commit 53bf4bc

Please sign in to comment.