Пример #1
0
 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);
 }
Пример #2
0
                } else {
                    $message = $update->message;
                    printf("  Got message: %s\n", $message->text);
                    if (!empty($message->from) && !empty($message->chat)) {
                        switch ($message->chat->type) {
                            case Chat::TYPE_PRIVATE:
                                printf("  Sending answer to a user %s\n", $message->chat->id);
                                $method = new SendMessage($message->chat->id, $message->text);
                                break;
                            case Chat::TYPE_GROUP:
                            case Chat::TYPE_SUPERGROUP:
                                printf("  Sending answer to a chat %s\n", $message->chat->id);
                                $method = (new SendMessage($message->chat->id, $message->text))->setReplyToMessageId($message->messageId);
                                break;
                        }
                        yield from $api->execute($method);
                    }
                }
            }
        }
    }
};
$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();
Пример #3
0
#!/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();