|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2015 Eric Enold <[email protected]> |
| 4 | + * |
| 5 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | + */ |
| 9 | +namespace Zyberspace\Telegram\Cli; |
| 10 | + |
| 11 | +//This class should actually be a trait and not abstract, but this way we can support PHP 5.3 and |
| 12 | +//don't rely on PHP 5.4 |
| 13 | + |
| 14 | +/** |
| 15 | + * Defines some command-wrappers, that get extended by the Client. |
| 16 | + */ |
| 17 | +abstract class AbstractClientCommands |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Sets status as online. |
| 21 | + * |
| 22 | + * @return boolean true on success, false otherwise |
| 23 | + * |
| 24 | + * @uses exec() |
| 25 | + */ |
| 26 | + public function setStatusOnline() |
| 27 | + { |
| 28 | + return $this->exec('status_online'); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Sets status as offline. |
| 33 | + * |
| 34 | + * @return boolean true on success, false otherwise |
| 35 | + * |
| 36 | + * @uses exec() |
| 37 | + */ |
| 38 | + public function setStatusOffline() |
| 39 | + { |
| 40 | + return $this->exec('status_offline'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Sends a text message to $peer. |
| 45 | + * |
| 46 | + * @param string $peer The peer, gets escaped with escapePeer(), |
| 47 | + * so you can directly use the values from getContactList() |
| 48 | + * @param string $msg The message to send, gets escaped with escapeStringArgument() |
| 49 | + * |
| 50 | + * @return boolean true on success, false otherwise |
| 51 | + * |
| 52 | + * @uses exec() |
| 53 | + * @uses escapePeer() |
| 54 | + * @uses escapeStringArgument() |
| 55 | + */ |
| 56 | + public function msg($peer, $msg) |
| 57 | + { |
| 58 | + $peer = $this->escapePeer($peer); |
| 59 | + $msg = $this->escapeStringArgument($msg); |
| 60 | + return $this->exec('msg ' . $peer . ' ' . $msg); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Adds a user to the contact list |
| 65 | + * |
| 66 | + * @param int|string $phoneNumber The phone-number of the new contact, needs to be a telegram-user. |
| 67 | + * Can start with or without '+'. |
| 68 | + * @param string $firstName The first name of the new contact |
| 69 | + * @param string $lastName The last name of the new contact |
| 70 | + * |
| 71 | + * @return string|boolean The new contact "$firstName $lastName"; false if somethings goes wrong |
| 72 | + * |
| 73 | + * @uses exec() |
| 74 | + * @uses escapeStringArgument() |
| 75 | + */ |
| 76 | + public function addContact($phoneNumber, $firstName, $lastName) |
| 77 | + { |
| 78 | + if (is_string($phoneNumber) && $phoneNumber[0] === '+') { |
| 79 | + $phoneNumber = substr($phoneNumber, 1); |
| 80 | + } |
| 81 | + $phoneNumber = (int) $phoneNumber; |
| 82 | + |
| 83 | + return $this->exec('add_contact ' . $phoneNumber . ' ' . $this->escapeStringArgument($firstName) |
| 84 | + . ' ' . $this->escapeStringArgument($lastName)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Renames a user in the contact list |
| 89 | + * |
| 90 | + * @param string $contact The contact, gets escaped with escapePeer(), |
| 91 | + * so you can directly use the values from getContactList() |
| 92 | + * @param string $firstName The new first name for the contact |
| 93 | + * @param string $lastName The new last name for the contact |
| 94 | + * |
| 95 | + * @return string|boolean The renamed contact "$firstName $lastName"; false if somethings goes wrong |
| 96 | + * |
| 97 | + * @uses exec() |
| 98 | + * @uses escapeStringArgument() |
| 99 | + */ |
| 100 | + public function renameContact($contact, $firstName, $lastName) |
| 101 | + { |
| 102 | + return $this->exec('rename_contact ' . $this->escapePeer($contact) |
| 103 | + . ' ' . $this->escapeStringArgument($firstName) . ' ' . $this->escapeStringArgument($lastName)); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Deletes a contact. |
| 108 | + * |
| 109 | + * @param string $contact The contact, gets escaped with escapePeer(), |
| 110 | + * so you can directly use the values from getContactList() |
| 111 | + * |
| 112 | + * @return boolean true on success, false otherwise |
| 113 | + * |
| 114 | + * @uses exec() |
| 115 | + * @uses escapePeer() |
| 116 | + */ |
| 117 | + public function deleteContact($contact) |
| 118 | + { |
| 119 | + return $this->exec('del_contact ' . $this->escapePeer($contact)); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Marks all messages with $peer as read. |
| 124 | + * |
| 125 | + * @param string $peer The peer, gets escaped with escapePeer(), |
| 126 | + * so you can directly use the values from getContactList() |
| 127 | + * |
| 128 | + * @return boolean true on success, false otherwise |
| 129 | + * |
| 130 | + * @uses exec() |
| 131 | + * @uses escapePeer() |
| 132 | + */ |
| 133 | + public function markRead($peer) |
| 134 | + { |
| 135 | + return $this->exec('mark_read ' . $this->escapePeer($peer)); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns an array of all contacts in form of "[firstName] [lastName]". |
| 140 | + * |
| 141 | + * @return array|boolean An array with your contacts; false if somethings goes wrong |
| 142 | + * |
| 143 | + * @uses exec() |
| 144 | + */ |
| 145 | + public function getContactList() |
| 146 | + { |
| 147 | + return explode(PHP_EOL, $this->exec('contact_list')); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Executes the user_info-command and returns it answer (the answer is unformated right now). |
| 152 | + * Will get better formated in the future. |
| 153 | + * |
| 154 | + * @param string $user The user, gets escaped with escapePeer(), |
| 155 | + * so you can directly use the values from getContactList() |
| 156 | + * |
| 157 | + * @return string|boolean The answer of the user_info-command; false if somethings goes wrong |
| 158 | + * |
| 159 | + * @uses exec() |
| 160 | + * @uses escapePeer() |
| 161 | + */ |
| 162 | + public function getUserInfo($user) |
| 163 | + { |
| 164 | + return $this->exec('user_info ' . $this->escapePeer($user)); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns an array of all your dialogs in form of |
| 169 | + * "User [firstName] [lastName]: [number of unread messages] unread". Will get better formated in the future. |
| 170 | + * |
| 171 | + * @return array|boolean An array with your dialogs; false if somethings goes wrong |
| 172 | + * |
| 173 | + * @uses exec() |
| 174 | + */ |
| 175 | + public function getDialogList() |
| 176 | + { |
| 177 | + return explode(PHP_EOL, $this->exec('dialog_list')); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Executes the history-command and returns it answer (the answer is unformated right now). |
| 182 | + * Will get better formated in the future. |
| 183 | + * |
| 184 | + * @param string $peer The peer, gets escaped with escapePeer(), |
| 185 | + * so you can directly use the values from getContactList() |
| 186 | + * @param int $limit (optional) Limit answer to $limit messages. If not set, there is no limit. |
| 187 | + * @param int $offset (optional) Use this with the $limit parameter to go through older messages. |
| 188 | + * Can also be negative. |
| 189 | + * |
| 190 | + * @return string|boolean The answer of the history-command; false if somethings goes wrong |
| 191 | + * |
| 192 | + * @uses exec() |
| 193 | + * @uses escapePeer() |
| 194 | + * |
| 195 | + * @see https://core.telegram.org/method/messages.getHistory |
| 196 | + */ |
| 197 | + public function getHistory($peer, $limit = null, $offset = null) |
| 198 | + { |
| 199 | + if ($limit !== null) { |
| 200 | + $limit = (int) $limit; |
| 201 | + if ($limit < 1) { //if limit is lesser than 1, telegram-cli crashes |
| 202 | + $limit = 1; |
| 203 | + } |
| 204 | + $limit = ' ' . $limit; |
| 205 | + } else { |
| 206 | + $limit = ''; |
| 207 | + } |
| 208 | + if ($offset !== null) { |
| 209 | + $offset = ' ' . (int) $offset; |
| 210 | + } else { |
| 211 | + $offset = ''; |
| 212 | + } |
| 213 | + |
| 214 | + return $this->exec('history ' . $this->escapePeer($peer) . $limit . $offset); |
| 215 | + } |
| 216 | +} |
0 commit comments