1: <?php
2:
3: /**
4: * Copyright 2016 LINE Corporation
5: *
6: * LINE Corporation licenses this file to you under the Apache License,
7: * version 2.0 (the "License"); you may not use this file except in compliance
8: * with the License. You may obtain a copy of the License at:
9: *
10: * https://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15: * License for the specific language governing permissions and limitations
16: * under the License.
17: */
18:
19: namespace LINE;
20:
21: use LINE\LINEBot\Event\Parser\EventRequestParser;
22: use LINE\LINEBot\HTTPClient;
23: use LINE\LINEBot\MessageBuilder;
24: use LINE\LINEBot\MessageBuilder\TextMessageBuilder;
25: use LINE\LINEBot\Response;
26: use LINE\LINEBot\SignatureValidator;
27:
28: /**
29: * A client class of LINE Messaging API.
30: *
31: * @package LINE
32: */
33: class LINEBot
34: {
35: const DEFAULT_ENDPOINT_BASE = 'https://api.line.me';
36:
37: /** @var string */
38: private $channelSecret;
39: /** @var string */
40: private $endpointBase;
41: /** @var HTTPClient */
42: private $httpClient;
43:
44: /**
45: * LINEBot constructor.
46: *
47: * @param HTTPClient $httpClient HTTP client instance to use API calling.
48: * @param array $args Configurations.
49: */
50: public function __construct(HTTPClient $httpClient, array $args)
51: {
52: $this->httpClient = $httpClient;
53: $this->channelSecret = $args['channelSecret'];
54:
55: $this->endpointBase = LINEBot::DEFAULT_ENDPOINT_BASE;
56: if (array_key_exists('endpointBase', $args) && !empty($args['endpointBase'])) {
57: $this->endpointBase = $args['endpointBase'];
58: }
59: }
60:
61: /**
62: * Gets specified user's profile through API calling.
63: *
64: * @param string $userId The user ID to retrieve profile.
65: * @return Response
66: */
67: public function getProfile($userId)
68: {
69: return $this->httpClient->get($this->endpointBase . '/v2/bot/profile/' . urlencode($userId));
70: }
71:
72: /**
73: * Gets message content which is associated with specified message ID.
74: *
75: * @param string $messageId The message ID to retrieve content.
76: * @return Response
77: */
78: public function getMessageContent($messageId)
79: {
80: return $this->httpClient->get($this->endpointBase . '/v2/bot/message/' . urlencode($messageId) . '/content');
81: }
82:
83: /**
84: * Replies arbitrary message to destination which is associated with reply token.
85: *
86: * @param string $replyToken Identifier of destination.
87: * @param MessageBuilder $messageBuilder Message builder to send.
88: * @return Response
89: */
90: public function replyMessage($replyToken, MessageBuilder $messageBuilder)
91: {
92: return $this->httpClient->post($this->endpointBase . '/v2/bot/message/reply', [
93: 'replyToken' => $replyToken,
94: 'messages' => $messageBuilder->buildMessage(),
95: ]);
96: }
97:
98: /**
99: * Replies text message(s) to destination which is associated with reply token.
100: *
101: * This method receives variable texts. It can send text(s) message as bulk.
102: *
103: * @param string $replyToken Identifier of destination.
104: * @param string $text Text of message.
105: * @param string[] $extraTexts Extra text of message.
106: * @return Response
107: */
108: public function replyText($replyToken, $text, ...$extraTexts)
109: {
110: $textMessageBuilder = new TextMessageBuilder($text, ...$extraTexts);
111: return $this->replyMessage($replyToken, $textMessageBuilder);
112: }
113:
114: /**
115: * Sends arbitrary message to destination.
116: *
117: * @param string $to Identifier of destination.
118: * @param MessageBuilder $messageBuilder Message builder to send.
119: * @return Response
120: */
121: public function pushMessage($to, MessageBuilder $messageBuilder)
122: {
123: return $this->httpClient->post($this->endpointBase . '/v2/bot/message/push', [
124: 'to' => $to,
125: 'messages' => $messageBuilder->buildMessage(),
126: ]);
127: }
128:
129: /**
130: * Sends arbitrary message to multi destinations.
131: *
132: * @param array $tos Identifiers of destination.
133: * @param MessageBuilder $messageBuilder Message builder to send.
134: * @return Response
135: */
136: public function multicast(array $tos, MessageBuilder $messageBuilder)
137: {
138: return $this->httpClient->post($this->endpointBase . '/v2/bot/message/multicast', [
139: 'to' => $tos,
140: 'messages' => $messageBuilder->buildMessage(),
141: ]);
142: }
143:
144: /**
145: * Leaves from group.
146: *
147: * @param string $groupId Identifier of group to leave.
148: * @return Response
149: */
150: public function leaveGroup($groupId)
151: {
152: return $this->httpClient->post($this->endpointBase . '/v2/bot/group/' . urlencode($groupId) . '/leave', []);
153: }
154:
155: /**
156: * Leaves from room.
157: *
158: * @param string $roomId Identifier of room to leave.
159: * @return Response
160: */
161: public function leaveRoom($roomId)
162: {
163: return $this->httpClient->post($this->endpointBase . '/v2/bot/room/' . urlencode($roomId) . '/leave', []);
164: }
165:
166: /**
167: * Parse event request to Event objects.
168: *
169: * @param string $body Request body.
170: * @param string $signature Signature of request.
171: * @return LINEBot\Event\BaseEvent[]
172: */
173: public function parseEventRequest($body, $signature)
174: {
175: return EventRequestParser::parseEventRequest($body, $this->channelSecret, $signature);
176: }
177:
178: /**
179: * Validate request with signature.
180: *
181: * @param string $body Request body.
182: * @param string $signature Signature of request.
183: * @return bool Request is valid or not.
184: */
185: public function validateSignature($body, $signature)
186: {
187: return SignatureValidator::validateSignature($body, $this->channelSecret, $signature);
188: }
189: }
190: