示例#1
0
 /**
  * Execute an API method.
  *
  * @param AbstractMethod $method
  *
  * @return \Generator
  * @throws TelegramBotApiException
  * @resolve object
  */
 public function execute(AbstractMethod $method) : \Generator
 {
     switch ($method->getHttpMethod()) {
         case $method::HTTP_GET:
             $response = (yield from $this->get('/' . $method->getMethodName(), $method->getParams()));
             break;
         case $method::HTTP_POST:
             if ($method instanceof \JsonSerializable) {
                 $body = json_encode($method);
                 $bodyStream = new MemoryStream(0, $body);
                 yield from $bodyStream->end();
                 $contentLength = mb_strlen($body);
             } else {
                 $bodyStream = null;
                 $contentLength = 0;
             }
             $headers = ['Content-Type' => 'application/json', 'Content-Length' => $contentLength];
             $response = (yield from $this->post('/' . $method->getMethodName(), $method->getParams(), $headers, $bodyStream));
             break;
     }
     $body = (yield from $this->getResponseBody($response));
     $body = json_decode($body, true);
     if ($body['ok'] === false) {
         throw new TelegramBotApiException($body['description'], $body['error_code']);
     }
     return $method->buildResult($body['result']);
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 protected function send(string $data, float $timeout = 0, bool $end = false) : \Generator
 {
     $length = strlen($data);
     if ($length) {
         $data = sprintf("%x\r\n%s\r\n", $length, $data);
     }
     if ($end) {
         $data .= "0\r\n\r\n";
     }
     return parent::send($data, $timeout, $end);
 }
示例#3
0
 /**
  * Send image to a user
  *
  * @var int         $chatId
  * @var resource    $photo
  * @var string|null $caption
  * @var int|null    $replyToMessageId
  * @var mixed       $replyMarkup
  *
  * @return \Generator
  */
 public function sendPhoto(int $chatId, $photo, string $caption = null, int $replyToMessageId = null, $replyMarkup = null) : \Generator
 {
     $params = ['chat_id' => $chatId, 'caption' => $caption, 'replyToMessageId' => $replyToMessageId];
     $imageContentPipe = new ReadablePipe($photo);
     $boundary = uniqid();
     $mem = new MemoryStream();
     $contentLength = 0;
     $contentLength += (yield $mem->write("--{$boundary}\r\nContent-Disposition: form-data; name=\"photo\"; filename=\"example1.jpg\"\r\n\r\n"));
     $contentLength += (yield pipe($imageContentPipe, $mem, false));
     $contentLength += (yield $mem->end("\r\n--{$boundary}--"));
     $url = $this->buildUrl('/sendPhoto', $params);
     $headers = ['Content-type' => "multipart/form-data, boundary={$boundary}", 'Content-Length' => $contentLength];
     $request = new Request('POST', $url, $headers, $mem);
     $response = (yield $this->httpClient->send($request, []));
     $body = (yield $this->getResponseBody($response));
     $body = json_decode($body, true);
     (yield new Entity\Message($body['result']));
 }