. * * @author Amir Hossein Jafari * @copyright 2016-2023 Amir Hossein Jafari * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 * @link https://docs.madelineproto.xyz MadelineProto documentation */ namespace danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler\Action\ChooseContact; use danog\MadelineProto\EventHandler\Action\ChooseSticker; use danog\MadelineProto\EventHandler\Action\EmojiSeen; use danog\MadelineProto\EventHandler\Action\EmojiTap; use danog\MadelineProto\EventHandler\Action\GamePlay; use danog\MadelineProto\EventHandler\Action\GeoLocation; use danog\MadelineProto\EventHandler\Action\GroupCallSpeaking; use danog\MadelineProto\EventHandler\Action\HistoryImport; use danog\MadelineProto\EventHandler\Action\RecordAudio; use danog\MadelineProto\EventHandler\Action\RecordRound; use danog\MadelineProto\EventHandler\Action\RecordVideo; use danog\MadelineProto\EventHandler\Action\Typing; use danog\MadelineProto\EventHandler\Action\UploadAudio; use danog\MadelineProto\EventHandler\Action\UploadDocument; use danog\MadelineProto\EventHandler\Action\UploadPhoto; use danog\MadelineProto\EventHandler\Action\UploadRound; use danog\MadelineProto\EventHandler\Action\UploadVideo; use JsonSerializable; use ReflectionClass; use ReflectionProperty; /** * Typing events */ abstract class AbstractAction implements JsonSerializable { /** * * * @param array $rawAction * @return AbstractAction */ public static function fromRawAction(array $rawAction): AbstractAction { $type = $rawAction['_']; if ($type === 'sendMessageEmojiInteraction') return new EmojiTap( $rawAction['emoticon'], $rawAction['interaction']['a']['t'], $rawAction['interaction']['a']['i'], $rawAction['msg_id'], ); return match ($type) { 'sendMessageTypingAction' => new Typing, 'sendMessageCancelAction' => new GamePlay, 'sendMessageGeoLocationAction' => new GeoLocation, 'sendMessageChooseContactAction' => new ChooseContact, 'sendMessageChooseStickerAction' => new ChooseSticker, 'sendMessageRecordAudioAction' => new RecordAudio, 'sendMessageRecordRoundAction' => new RecordRound, 'sendMessageRecordVideoAction' => new RecordVideo, 'speakingInGroupCallAction' => new GroupCallSpeaking, 'sendMessageUploadVideoAction' => new UploadVideo($rawAction['progress']), 'sendMessageUploadRoundAction' => new UploadRound($rawAction['progress']), 'sendMessageUploadAudioAction' => new UploadAudio($rawAction['progress']), 'sendMessageUploadPhotoAction' => new UploadPhoto($rawAction['progress']), 'sendMessageUploadDocumentAction' => new UploadDocument($rawAction['progress']), 'sendMessageHistoryImportAction' => new HistoryImport($rawAction['progress']), 'sendMessageEmojiInteractionSeen' => new EmojiSeen($rawAction['emoticon']), }; } public function toRawAction(): array { return match (true) { $this instanceof Typing => [ '_' => 'sendMessageTypingAction' ], $this instanceof GamePlay => [ '_' => 'sendMessageCancelAction' ], $this instanceof GeoLocation => [ '_' => 'sendMessageGeoLocationAction' ], $this instanceof ChooseContact => [ '_' => 'sendMessageChooseContactAction' ], $this instanceof ChooseSticker => [ '_' => 'sendMessageChooseStickerAction' ], $this instanceof RecordAudio => [ '_' => 'sendMessageRecordAudioAction' ], $this instanceof RecordRound => [ '_' => 'sendMessageRecordRoundAction' ], $this instanceof RecordVideo => [ '_' => 'sendMessageRecordVideoAction' ], $this instanceof UploadVideo => [ '_' => 'sendMessageUploadVideoAction'], $this instanceof UploadRound => [ '_' => 'sendMessageUploadRoundAction'], $this instanceof UploadAudio => [ '_' => 'sendMessageUploadAudioAction'], $this instanceof UploadPhoto => [ '_' => 'sendMessageUploadPhotoAction'], $this instanceof UploadDocument => [ '_' => 'sendMessageUploadDocumentAction'], $this instanceof HistoryImport => [ '_' => 'sendMessageHistoryImportAction'], $this instanceof GroupCallSpeaking => [ '_' => 'speakingInGroupCallAction' ], $this instanceof EmojiSeen => [ '_' => 'sendMessageEmojiInteractionSeen' ], $this instanceof EmojiTap => [ '_' => 'sendMessageEmojiInteraction' ], }; } /** @internal */ public function jsonSerialize(): mixed { $res = ['_' => static::class]; $refl = new ReflectionClass($this); foreach ($refl->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) { $res[$prop->getName()] = $prop->getValue($this); } return $res; } }