Skip to content

Commit

Permalink
Add event group_chat_created (#47)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Lloople authored and mpociot committed Jul 30, 2018
1 parent cba2774 commit 32ed131
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/TelegramDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions tests/TelegramDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 32ed131

Please sign in to comment.