Skip to content

Commit

Permalink
Merge pull request #678 from defstudio/implement-InlineQueryResultVenue
Browse files Browse the repository at this point in the history
Implement InlineQueryResultVenue
  • Loading branch information
fabio-ivona authored Dec 17, 2024
2 parents 05df94e + 4ad0b71 commit 60ddbcc
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 3 deletions.
6 changes: 5 additions & 1 deletion docs/12.features/9.dto.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ This is a DTO for outgoing data, wraps info about the Document result returned t

This is a DTO for outgoing data, wraps info about the Location result returned to the user

## `InlineQueryResultVenue`

This is a DTO for outgoing data, wraps info about the Venue result returned to the user

## `ChatInviteLink`

represents an invite link for a chat.
Expand All @@ -259,4 +263,4 @@ represents a join request sent to a chat.
- `->userChatId()` identifier of a private chat with the user who sent the join request. This number may have more than 32 significant bits and some programming languages may have difficulty/silent defects in interpreting it. But it has at most 52 significant bits, so a 64-bit integer or double-precision float type are safe for storing this identifier. The bot can use this identifier for 5 minutes to send messages until the join request is processed, assuming no other administrator contacted the user.
- `->date()` date the request was sent in Unix time
- `->bio()` (optional) bio of the user
- `->inviteLink()` (optional) an instance of [`ChatInviteLink`](#chat-invite-link) that was used by the user to send the join request
- `->inviteLink()` (optional) an instance of [`ChatInviteLink`](#chat-invite-link) that was used by the user to send the join request
4 changes: 2 additions & 2 deletions docs/15.webhooks/4.webhook-request-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ Different kind of result can be sent through the handler:
- Gif ([`DefStudio\Telegraph\DTO\InlineQueryResultGif`](features/dto#inline-query-result-gif))
- Location ([`DefStudio\Telegraph\DTO\InlineQueryResultLocation`](features/dto#inline-query-result-location))
- Mpeg4Gif ([`DefStudio\Telegraph\DTO\InlineQueryResultMpeg4Gif`](features/dto#inline-query-result-Mpeg4Gif))
- Photo([`DefStudio\Telegraph\DTO\InlineQueryResultPhoto`](features/dto#inline-query-result-photo))
- Venue (coming soon)
- Photo ([`DefStudio\Telegraph\DTO\InlineQueryResultPhoto`](features/dto#inline-query-result-photo))
- Venue ([`DefStudio\Telegraph\DTO\InlineQueryResultVenue`](features/dto#inline-query-result-venue))
- Video ([`DefStudio\Telegraph\DTO\InlineQueryResultVideo`](features/dto#inline-query-result-video))
- Voice ([`DefStudio\Telegraph\DTO\InlineQueryResultVoice`](features/dto#inline-query-result-voice))

Expand Down
116 changes: 116 additions & 0 deletions src/DTO/InlineQueryResultVenue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/** @noinspection PhpUnused */

namespace DefStudio\Telegraph\DTO;

use DefStudio\Telegraph\Telegraph;

class InlineQueryResultVenue extends InlineQueryResult
{
protected string $type = 'venue';
protected string $id;
protected string $title;
protected float $latitude;
protected float $longitude;
protected string $address;
protected string|null $foursquareId = null;
protected string|null $foursquareType = null;
protected string|null $googlePlaceId = null;
protected string|null $googlePlaceType = null;
protected string|null $message = null;
protected string|null $thumbUrl = null;
protected int|null $thumbWidth = null;
protected int|null $thumbHeight = null;

public static function make(string $id, string $title, float $latitude, float $longitude, string $address, string $message = null): InlineQueryResultVenue
{
$result = new InlineQueryResultVenue();
$result->id = $id;
$result->title = $title;
$result->latitude = $latitude;
$result->longitude = $longitude;
$result->address = $address;
$result->message = $message;

return $result;
}

public function thumbUrl(string|null $thumbUrl): static
{
$this->thumbUrl = $thumbUrl;

return $this;
}

public function thumbWidth(int|null $thumbWidth): static
{
$this->thumbWidth = $thumbWidth;

return $this;
}

public function thumbHeight(int|null $thumbHeight): static
{
$this->thumbHeight = $thumbHeight;

return $this;
}

public function foursquareId(string|null $foursquareId): static
{
$this->foursquareId = $foursquareId;

return $this;
}

public function foursquareType(string|null $foursquareType): static
{
$this->foursquareType = $foursquareType;

return $this;
}

public function googlePlaceId(string|null $googlePlaceId): static
{
$this->googlePlaceId = $googlePlaceId;

return $this;
}

public function googlePlaceType(string|null $googlePlaceType): static
{
$this->googlePlaceType = $googlePlaceType;

return $this;
}

/**
* @inheritDoc
*/
public function data(): array
{
$data = [
'title' => $this->title,
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'address' => $this->address,
'foursquare_id' => $this->foursquareId,
'foursquare_type' => $this->foursquareType,
'google_place_id' => $this->googlePlaceId,
'google_place_type' => $this->googlePlaceType,
'thumb_url' => $this->thumbUrl,
'thumb_width' => $this->thumbWidth,
'thumb_height' => $this->thumbHeight,
];

if ($this->message !== null) {
$data['input_message_content'] = [
'message_text' => $this->message,
'parse_mode' => $this->parseMode ?? config('telegraph.default_parse_mode', Telegraph::PARSE_HTML),
];
}

return array_filter($data, fn ($value) => $value !== null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
"longitude": 10.5,
"id": "42",
"type": "location"
},
{
"title": "testVenueTitle",
"latitude": 10.5,
"longitude": 10.5,
"address": "testAddress",
"id": "42",
"type": "venue"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
"longitude": 10.5,
"id": "42",
"type": "location"
},
{
"title": "testVenueTitle",
"latitude": 10.5,
"longitude": 10.5,
"address": "testAddress",
"id": "42",
"type": "venue"
}
],
"switch_pm_text": "configure",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
"longitude": 10.5,
"id": "42",
"type": "location"
},
{
"title": "testVenueTitle",
"latitude": 10.5,
"longitude": 10.5,
"address": "testAddress",
"id": "42",
"type": "venue"
}
],
"cache_time": 600
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
"longitude": 10.5,
"id": "42",
"type": "location"
},
{
"title": "testVenueTitle",
"latitude": 10.5,
"longitude": 10.5,
"address": "testAddress",
"id": "42",
"type": "venue"
}
],
"next_offset": "2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
"longitude": 10.5,
"id": "42",
"type": "location"
},
{
"title": "testVenueTitle",
"latitude": 10.5,
"longitude": 10.5,
"address": "testAddress",
"id": "42",
"type": "venue"
}
],
"is_personal": true
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Concerns/AnswersInlineQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use DefStudio\Telegraph\DTO\InlineQueryResultLocation;
use DefStudio\Telegraph\DTO\InlineQueryResultMpeg4Gif;
use DefStudio\Telegraph\DTO\InlineQueryResultPhoto;
use DefStudio\Telegraph\DTO\InlineQueryResultVenue;
use DefStudio\Telegraph\DTO\InlineQueryResultVideo;
use DefStudio\Telegraph\DTO\InlineQueryResultVoice;
use DefStudio\Telegraph\Exceptions\InlineQueryException;
Expand All @@ -27,6 +28,7 @@
InlineQueryResultVoice::make(42, 'testVoiceUrl', 'testTitle'),
InlineQueryResultDocument::make(42, 'testDocumentTitle', 'testDocumentUrl', 'testDocumentMimeType'),
InlineQueryResultLocation::make(42, 'testLocationTitle', 10.5, 10.5),
InlineQueryResultVenue::make(42, 'testVenueTitle', 10.5, 10.5, 'testAddress'),
]))->toMatchTelegramSnapshot();
});

Expand All @@ -43,6 +45,7 @@
InlineQueryResultVoice::make(42, 'testVoiceUrl', 'testTitle'),
InlineQueryResultDocument::make(42, 'testDocumentTitle', 'testDocumentUrl', 'testDocumentMimeType'),
InlineQueryResultLocation::make(42, 'testLocationTitle', 10.5, 10.5),
InlineQueryResultVenue::make(42, 'testVenueTitle', 10.5, 10.5, 'testAddress'),
])->cache(600)
)->toMatchTelegramSnapshot();
});
Expand All @@ -60,6 +63,7 @@
InlineQueryResultVoice::make(42, 'testVoiceUrl', 'testTitle'),
InlineQueryResultDocument::make(42, 'testDocumentTitle', 'testDocumentUrl', 'testDocumentMimeType'),
InlineQueryResultLocation::make(42, 'testLocationTitle', 10.5, 10.5),
InlineQueryResultVenue::make(42, 'testVenueTitle', 10.5, 10.5, 'testAddress'),
])->nextOffset('2')
)->toMatchTelegramSnapshot();
});
Expand All @@ -77,6 +81,7 @@
InlineQueryResultVoice::make(42, 'testVoiceUrl', 'testTitle'),
InlineQueryResultDocument::make(42, 'testDocumentTitle', 'testDocumentUrl', 'testDocumentMimeType'),
InlineQueryResultLocation::make(42, 'testLocationTitle', 10.5, 10.5),
InlineQueryResultVenue::make(42, 'testVenueTitle', 10.5, 10.5, 'testAddress'),
])->personal()
)->toMatchTelegramSnapshot();
});
Expand All @@ -94,6 +99,7 @@
InlineQueryResultVoice::make(42, 'testVoiceUrl', 'testTitle'),
InlineQueryResultDocument::make(42, 'testDocumentTitle', 'testDocumentUrl', 'testDocumentMimeType'),
InlineQueryResultLocation::make(42, 'testLocationTitle', 10.5, 10.5),
InlineQueryResultVenue::make(42, 'testVenueTitle', 10.5, 10.5, 'testAddress'),
])->offertToSwitchToPrivateMessage('configure', '123456')
)->toMatchTelegramSnapshot();
});
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/DTO/InlineQueryResultVenueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use DefStudio\Telegraph\DTO\InlineQueryResultVenue;

it('can export to array', function () {
expect(
InlineQueryResultVenue::make('a45', 'testTitle', 10.5, 10.5, 'testAddress')
->thumbUrl('testThumbUrl')
->thumbWidth(200)
->thumbHeight(100)
->foursquareId('testId')
->foursquareType('testType')
->googlePlaceId('testPlaceId')
->googlePlaceType('testPlaceType')
->toArray()
)->
toBe([
'title' => 'testTitle',
'latitude' => 10.5,
'longitude' => 10.5,
'address' => 'testAddress',
'foursquare_id' => 'testId',
'foursquare_type' => 'testType',
'google_place_id' => 'testPlaceId',
'google_place_type' => 'testPlaceType',
'thumb_url' => 'testThumbUrl',
'thumb_width' => 200,
'thumb_height' => 100,
'id' => "a45",
'type' => "venue",
]);
});

0 comments on commit 60ddbcc

Please sign in to comment.