public function testExecuteWithJsonBody() { $method = $this->createMock(AbstractMethodWithBodyStub::class); $method->expects($this->once())->method('getMethodName')->willReturn('/someMethod'); $method->expects($this->once())->method('getHttpMethod')->willReturn('POST'); $method->expects($this->once())->method('getParams')->willReturn(['param1' => 'value1', 'param2' => 'value2']); $method->expects($this->once())->method('buildResult')->willReturn('result'); $method->expects($this->once())->method('jsonSerialize')->willReturn(['jsonParam1' => 'jsonValue1', 'jsonParam2' => 'jsonValue2']); $responseData = json_encode(['ok' => true, 'result' => 'result'], JSON_UNESCAPED_UNICODE); $this->setUpHttpClient($responseData); $api = new Api($this->telegramToken, $this->httpClient); $coroutine = new Coroutine($api->execute($method)); $result = $coroutine->wait(); $this->assertEquals('result', $result); }
#!/usr/bin/env php <?php require dirname(__DIR__) . '/vendor/autoload.php'; use Icicle\Loop; use Steelbot\TelegramBotApi\Api; use Steelbot\TelegramBotApi\Method\SendMessage; use Steelbot\TelegramBotApi\Type\Chat; use Steelbot\TelegramBotApi\Type\Update; if (!getenv('BOT_TOKEN')) { echo "Error: BOT_TOKEN environment variable not found\n"; printf("Usage:\n BOT_TOKEN=123:telegram_bot_token ./%s\n", basename(__FILE__)); exit(-1); } $generator = function () { $api = new Api(getenv('BOT_TOKEN')); $updateId = 1; while (true) { // waiting for updates from telegram server /** @var Update[] $updates */ $updates = (yield from $api->getUpdates($updateId)); foreach ($updates as $update) { $updateId = $update->updateId; printf("Got update #%d\n", $updateId); if (!empty($update->message)) { if (empty($update->message->text)) { printf(" Message text is empty\n"); } else { $message = $update->message; printf(" Got message: %s\n", $message->text); if (!empty($message->from) && !empty($message->chat)) { switch ($message->chat->type) {
#!/usr/bin/env php <?php require dirname(__DIR__) . '/vendor/autoload.php'; use Steelbot\TelegramBotApi\Api; use Icicle\Loop; use Steelbot\TelegramBotApi\Method\SendMessage; if (!getenv('BOT_TOKEN')) { echo "Error: BOT_TOKEN environment variable not found\n"; printf("Usage:\n BOT_TOKEN=123:telegram_bot_token ./%s\n", basename(__FILE__)); exit(-1); } $generator = function () : \Generator { $api = new Api(getenv('BOT_TOKEN')); $method = new SendMessage('104442434', 'Hello, world!'); $message = (yield from $api->execute($method)); echo "Message was sent:\n"; print_r($message); }; $coroutine = new \Icicle\Coroutine\Coroutine($generator()); $coroutine->done(null, function (\Throwable $exception) { echo "Exception catched:\n"; echo " Code: {$exception->getCode()}\n"; echo " Message: {$exception->getMessage()}\n"; echo " File: {$exception->getFile()}\n"; echo " Line: {$exception->getLine()}\n"; }); Loop\run();