vendor/botman/botman/src/Messages/Incoming/IncomingMessage.php line 260

Open in your IDE?
  1. <?php
  2. namespace BotMan\BotMan\Messages\Incoming;
  3. use BotMan\BotMan\Messages\Attachments\Contact;
  4. use BotMan\BotMan\Messages\Attachments\Location;
  5. use Illuminate\Support\Collection;
  6. class IncomingMessage
  7. {
  8. /** @var string */
  9. protected $message;
  10. /** @var string */
  11. protected $sender;
  12. /** @var string */
  13. protected $recipient;
  14. /** @var string */
  15. protected $bot_id;
  16. /** @var array */
  17. protected $images = [];
  18. /** @var array */
  19. protected $videos = [];
  20. /** @var mixed */
  21. protected $payload;
  22. /** @var array */
  23. protected $extras = [];
  24. /** @var array */
  25. private $audio = [];
  26. /** @var array */
  27. private $files = [];
  28. /** @var \BotMan\BotMan\Messages\Attachments\Location */
  29. private $location;
  30. /** @var \BotMan\BotMan\Messages\Attachments\Contact */
  31. private $contact;
  32. /** @var bool */
  33. protected $isFromBot = false;
  34. public function __construct($message, $sender, $recipient, $payload = null, $bot_id = '')
  35. {
  36. $this->message = $message;
  37. $this->sender = $sender;
  38. $this->recipient = $recipient;
  39. $this->payload = $payload;
  40. $this->bot_id = $bot_id;
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function getRecipient()
  46. {
  47. return $this->recipient;
  48. }
  49. /**
  50. * @return string
  51. */
  52. public function getSender()
  53. {
  54. return $this->sender;
  55. }
  56. /**
  57. * @return mixed
  58. */
  59. public function getPayload()
  60. {
  61. return $this->payload;
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function getText()
  67. {
  68. return $this->message;
  69. }
  70. /**
  71. * @return string
  72. */
  73. public function getConversationIdentifier()
  74. {
  75. return 'conversation-' . $this->bot_id . sha1((string)$this->getSender()) . '-' . sha1((string)$this->getRecipient());
  76. }
  77. /**
  78. * We don't know the user, since conversations are originated on the channel.
  79. *
  80. * @return string
  81. */
  82. public function getOriginatedConversationIdentifier()
  83. {
  84. return 'conversation-' . $this->bot_id . sha1((string)$this->getSender()) . '-' . sha1('');
  85. }
  86. /**
  87. * @param string $key
  88. * @param mixed $value
  89. * @return IncomingMessage
  90. */
  91. public function addExtras($key, $value)
  92. {
  93. $this->extras[$key] = $value;
  94. return $this;
  95. }
  96. /**
  97. * @param string|null $key
  98. * @return mixed
  99. */
  100. public function getExtras($key = null)
  101. {
  102. if (!is_null($key)) {
  103. return Collection::make($this->extras)->get($key);
  104. }
  105. return $this->extras;
  106. }
  107. /**
  108. * @param array $images
  109. */
  110. public function setImages(array $images)
  111. {
  112. $this->images = $images;
  113. }
  114. /**
  115. * Returns the message image Objects.
  116. * @return array
  117. */
  118. public function getImages()
  119. {
  120. return $this->images;
  121. }
  122. /**
  123. * @param array $videos
  124. */
  125. public function setVideos(array $videos)
  126. {
  127. $this->videos = $videos;
  128. }
  129. /**
  130. * Returns the message video Objects.
  131. * @return array
  132. */
  133. public function getVideos()
  134. {
  135. return $this->videos;
  136. }
  137. /**
  138. * @param array $audio
  139. */
  140. public function setAudio(array $audio)
  141. {
  142. $this->audio = $audio;
  143. }
  144. /**
  145. * Returns the message audio Objects.
  146. * @return array
  147. */
  148. public function getAudio()
  149. {
  150. return $this->audio;
  151. }
  152. /**
  153. * @param array $files
  154. */
  155. public function setFiles(array $files)
  156. {
  157. $this->files = $files;
  158. }
  159. /**
  160. * @return array
  161. */
  162. public function getFiles()
  163. {
  164. return $this->files;
  165. }
  166. /**
  167. * @param \BotMan\BotMan\Messages\Attachments\Location $location
  168. */
  169. public function setLocation(Location $location)
  170. {
  171. $this->location = $location;
  172. }
  173. /**
  174. * @return \BotMan\BotMan\Messages\Attachments\Location
  175. */
  176. public function getLocation(): Location
  177. {
  178. if (empty($this->location)) {
  179. throw new \UnexpectedValueException('This message does not contain a location');
  180. }
  181. return $this->location;
  182. }
  183. /**
  184. * @return \BotMan\BotMan\Messages\Attachments\Contact
  185. */
  186. public function getContact(): Contact
  187. {
  188. if (empty($this->contact)) {
  189. throw new \UnexpectedValueException('This message does not contain a contact');
  190. }
  191. return $this->contact;
  192. }
  193. /**
  194. * @param \BotMan\BotMan\Messages\Attachments\Contact $contact
  195. */
  196. public function setContact(Contact $contact)
  197. {
  198. $this->contact = $contact;
  199. }
  200. /**
  201. * @return bool
  202. */
  203. public function isFromBot(): bool
  204. {
  205. return $this->isFromBot;
  206. }
  207. /**
  208. * @param bool $isFromBot
  209. */
  210. public function setIsFromBot(bool $isFromBot)
  211. {
  212. $this->isFromBot = $isFromBot;
  213. }
  214. /**
  215. * @param string $message
  216. */
  217. public function setText(string $message)
  218. {
  219. $this->message = $message;
  220. }
  221. }