From 32ed131c73ee09791e320557340e663d90d7b45d Mon Sep 17 00:00:00 2001 From: David Llop Date: Mon, 30 Jul 2018 22:18:18 +0200 Subject: [PATCH] Add event group_chat_created (#47) * Be able to send venue information * Add generic event group_chat_created and tests. Also refactored the hasMatchingEvent method to reduce the code lines and magic strings. --- src/TelegramDriver.php | 30 +++++++++++++----------------- tests/TelegramDriverTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/TelegramDriver.php b/src/TelegramDriver.php index 0745a6b..7306ab6 100644 --- a/src/TelegramDriver.php +++ b/src/TelegramDriver.php @@ -26,6 +26,13 @@ class TelegramDriver extends HttpDriver const API_URL = 'https://api.telegram.org/bot'; const FILE_API_URL = 'https://api.telegram.org/file/bot'; const LOGIN_EVENT = 'telegram_login'; + const GENERIC_EVENTS = [ + 'new_chat_members', + 'left_chat_member', + 'new_chat_title', + 'new_chat_photo', + 'group_chat_created' + ]; protected $endpoint = 'sendMessage'; @@ -102,24 +109,13 @@ public function hasMatchingEvent() $event->setName(self::LOGIN_EVENT); } - if ($this->event->has('new_chat_members')) { - $event = new GenericEvent($this->event->get('new_chat_members')); - $event->setName('new_chat_members'); - } - - if ($this->event->has('left_chat_member')) { - $event = new GenericEvent($this->event->get('left_chat_member')); - $event->setName('left_chat_member'); - } + foreach (self::GENERIC_EVENTS as $genericEvent) { + if ($this->event->has($genericEvent)) { + $event = new GenericEvent($this->event->get($genericEvent)); + $event->setName($genericEvent); - if ($this->event->has('new_chat_title')) { - $event = new GenericEvent($this->event->get('new_chat_title')); - $event->setName('new_chat_title'); - } - - if ($this->event->has('new_chat_photo')) { - $event = new GenericEvent($this->event->get('new_chat_photo')); - $event->setName('new_chat_photo'); + return $event; + } } return $event; diff --git a/tests/TelegramDriverTest.php b/tests/TelegramDriverTest.php index 0d31b83..6f530df 100644 --- a/tests/TelegramDriverTest.php +++ b/tests/TelegramDriverTest.php @@ -179,6 +179,30 @@ public function it_calls_new_chat_member_event() $this->assertSame('Marcel', $event->getPayload()[0]['first_name']); } + /** @test */ + public function it_calls_group_chat_created_event() + { + $driver = $this->getDriver([ + 'update_id' => '1234567890', + 'message' => [ + 'message_id' => '123', + 'from' => [ + 'id' => 'from_id', + ], + 'chat' => [ + 'id' => 'chat_id', + ], + 'date' => '1480369277', + 'text' => 'Hi Julia', + 'group_chat_created' => true, + ], + ]); + $event = $driver->hasMatchingEvent(); + $this->assertInstanceOf(GenericEvent::class, $event); + $this->assertSame('group_chat_created', $event->getName()); + $this->assertTrue($event->getPayload()); + } + /** @test */ public function it_calls_left_chat_member_event() {