Skip to content

Commit

Permalink
Merge pull request #388 from sepgg/main
Browse files Browse the repository at this point in the history
access to web_app_date in message
  • Loading branch information
fabio-ivona authored Jun 20, 2023
2 parents 6b20fa1 + 4394a28 commit 8f8a68c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/content/en/webhooks/dto.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ contains incoming data (a message or a callback query)
- `->voice()` (optional) an instance of [`DefStudio\Telegraph\DTO\Voice`](webhooks/dto#defstudio-telegraph-dto-voice) holding data about the contained voical message
- `->newChatMembers()` a collection of [`DefStudio\Telegraph\DTO\User`](webhooks/dto#defstudio-telegraph-dto-user) holding the list of users that joined the group/supergroup
- `->leftChatMember()` (optional) an instance of [`DefStudio\Telegraph\DTO\User`](webhooks/dto#defstudio-telegraph-dto-user) holding data about the user that left the group/supergroup
- `->webAppData()` (optional) incoming data from sendData method of telegram WebApp



Expand Down
19 changes: 19 additions & 0 deletions src/DTO/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Message implements Arrayable
private ?CarbonInterface $editDate = null;

private string $text;
/** Can be string or json string. if json then convert it to array */
private mixed $webAppData = null;
private bool $protected = false;

private ?User $from = null;
Expand Down Expand Up @@ -72,6 +74,7 @@ private function __construct()
* contact?: array<string, mixed>,
* new_chat_members?: array<string, mixed>,
* left_chat_member?: array<string, mixed>,
* web_app_data?: string
* } $data
*/
public static function fromArray(array $data): Message
Expand Down Expand Up @@ -165,6 +168,16 @@ public static function fromArray(array $data): Message
$message->leftChatMember = User::fromArray($data['left_chat_member']);
}

if (isset($data['web_app_data']['data'])) {
$webAppData = json_decode($data['web_app_data']['data'], true);

if(!$webAppData) {
$webAppData = $data['web_app_data']['data'];
}

$message->webAppData = $webAppData;
}

return $message;
}

Expand Down Expand Up @@ -274,6 +287,11 @@ public function leftChatMember(): ?User
return $this->leftChatMember;
}

public function webAppData(): mixed
{
return $this->webAppData;
}

public function toArray(): array
{
return array_filter([
Expand All @@ -297,6 +315,7 @@ public function toArray(): array
'voice' => $this->voice?->toArray(),
'new_chat_members' => $this->newChatMembers->toArray(),
'left_chat_member' => $this->leftChatMember,
'web_app_data' => $this->webAppData,
]);
}
}
47 changes: 47 additions & 0 deletions tests/Unit/DTO/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@
'first_name' => 'Bob',
],
],
'web_app_data' => [
"button" => "CustomButton",
"data" => "Data"
]
]);

$array = $dto->toArray();
Expand All @@ -255,3 +259,46 @@
expect($array)->toHaveKey(Str::of($property->name)->snake());
}
});

it("extract web_app_data of string type", function(){
$dto = Message::fromArray([
'message_id' => 2,
'date' => now()->timestamp,
'web_app_data' => [
"button" => "SendString",
"data" => "Data"
]
]);
$webAppData = $dto->webAppData();

expect($webAppData)->toBe("Data");
});


it("extract web_app_data of json type", function(){
$dto = Message::fromArray([
'message_id' => 2,
'date' => now()->timestamp,
'web_app_data' => [
"button" => "SendJson",
"data" => '[
false,
1,
"string",
{
"a" : "b"
}
]'
]
]);
$webAppData = $dto->webAppData();

expect($webAppData)->toBe([
false,
1,
"string",
[
"a" => "b"
]
]);
});

0 comments on commit 8f8a68c

Please sign in to comment.