send() public method

public send ( Request $request, array $options = [] ) : Generator
$request Request
$options array
return Generator
Beispiel #1
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']));
 }