forked from zelenin/telegram-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
70 lines (61 loc) · 1.63 KB
/
Copy pathClient.php
File metadata and controls
70 lines (61 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Zelenin\Telegram\Bot;
use GuzzleHttp\Exception\ClientException;
class Client implements ClientInterface
{
/**
* @var string
*/
private $baseUrl = 'https://api.telegram.org/bot{token}/{method}';
/**
* @var string
*/
private $token;
/**
* @param string $token
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* @param string $method
* @param array $params
*
* @return Response
*/
public function request($method, $params = [])
{
$client = new \GuzzleHttp\Client();
$multipartParams = [];
foreach ($params as $key => $value) {
$multipartParams[] = [
'name' => $key,
'contents' => is_scalar($value) ? (string)$value : $value
];
}
try {
$response = $client->post($this->getUrl($method), [
'verify' => false,
'multipart' => $multipartParams ?: null
]);
$response = json_decode($response->getBody());
return new Response($response->ok, $response->result);
} catch (ClientException $e) {
$response = json_decode($e->getResponse()->getBody()->getContents());
return new Response($response->ok, null, $response->error_code, $response->description);
}
}
/**
* @param string $method
*
* @return string
*/
private function getUrl($method)
{
return strtr($this->baseUrl, [
'{token}' => $this->token,
'{method}' => $method
]);
}
}