|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2025 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\LINEBot\KitchenSink\EventHandler; |
| 20 | + |
| 21 | +use LINE\Clients\MessagingApi\Api\MessagingApiApi; |
| 22 | +use LINE\Clients\MessagingApi\Model\ReplyMessageRequest; |
| 23 | +use LINE\Clients\MessagingApi\Model\TextMessage; |
| 24 | +use LINE\Constants\MessageType; |
| 25 | +use LINE\LINEBot\KitchenSink\EventHandler; |
| 26 | +use LINE\Webhook\Model\GroupSource; |
| 27 | +use LINE\Webhook\Model\JoinEvent; |
| 28 | +use LINE\Webhook\Model\MemberJoinedEvent; |
| 29 | +use LINE\Webhook\Model\RoomSource; |
| 30 | + |
| 31 | +class MemberJoinedEventHandler implements EventHandler |
| 32 | +{ |
| 33 | + /** @var MessagingApiApi $bot */ |
| 34 | + private $bot; |
| 35 | + /** @var \Psr\Log\LoggerInterface $logger */ |
| 36 | + private $logger; |
| 37 | + /** @var MemberJoinedEvent $memberJoinedEvent */ |
| 38 | + private $memberJoinedEvent; |
| 39 | + |
| 40 | + /** |
| 41 | + * JoinEventHandler constructor. |
| 42 | + * @param MessagingApiApi $bot |
| 43 | + * @param \Psr\Log\LoggerInterface $logger |
| 44 | + * @param MemberJoinedEvent $memberJoinedEvent |
| 45 | + */ |
| 46 | + public function __construct(MessagingApiApi $bot, \Psr\Log\LoggerInterface $logger, MemberJoinedEvent $memberJoinedEvent) |
| 47 | + { |
| 48 | + $this->bot = $bot; |
| 49 | + $this->logger = $logger; |
| 50 | + $this->memberJoinedEvent = $memberJoinedEvent; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @throws LINEBot\Exception\InvalidEventSourceException |
| 55 | + * @throws \ReflectionException|\LINE\Clients\MessagingApi\ApiException |
| 56 | + */ |
| 57 | + public function handle() |
| 58 | + { |
| 59 | + $source = $this->memberJoinedEvent->getSource(); |
| 60 | + if ($source instanceof GroupSource) { |
| 61 | + $id = $source->getGroupId(); |
| 62 | + } elseif ($source instanceof RoomSource) { |
| 63 | + $id = $source->getRoomId(); |
| 64 | + } else { |
| 65 | + $this->logger->error("Unknown event type"); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $joinedMembers = $this->memberJoinedEvent->getJoined()->getMembers(); |
| 70 | + $joinedMemberIds = array_map(function ($member) { |
| 71 | + return $member["userId"]; |
| 72 | + }, $joinedMembers); |
| 73 | + |
| 74 | + $request = new ReplyMessageRequest([ |
| 75 | + 'replyToken' => $this->memberJoinedEvent->getReplyToken(), |
| 76 | + 'messages' => [ |
| 77 | + new TextMessage([ |
| 78 | + 'type' => MessageType::TEXT, |
| 79 | + 'text' => sprintf('%s joined. %s %s', implode(", ", $joinedMemberIds), $this->memberJoinedEvent->getType(), $id), |
| 80 | + ]), |
| 81 | + ], |
| 82 | + ]); |
| 83 | + $this->bot->replyMessage($request); |
| 84 | + } |
| 85 | +} |
0 commit comments