-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix update types error, Add inline queries test
- Loading branch information
Showing
6 changed files
with
179 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |