├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── discovery-shell.php ├── example.php ├── lib └── Zyberspace │ └── Telegram │ └── Cli │ ├── Client.php │ ├── ClientException.php │ └── RawClient.php └── phpdoc.dist.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.lock 2 | /.developer-shell-history 3 | /vendor/ 4 | /doc/ 5 | /.project 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ###Update 2015-12-23: 2 | 3 | **As i currently have no time to work on this project anymore and telegram released its own [bot-api](https://core.telegram.org/bots), there won't be many new updates to this project in the future. I will try to go through all currently open issues so there is nothing left in the issue section anymore (even if i need to close some things with `wontfix`). 4 | If someone wants to takeover this project i am willing to give him push-rights or link to his fork on github/packagist.** 5 | 6 | **If you want to port your project to the new bot-api take a look at the [unofficial php-sdk](https://github.com/irazasyed/telegram-bot-sdk).** 7 | 8 |
9 | 10 | zyberspace/telegram-cli-client 11 | ============================== 12 | php-client for [telegram-cli](https://github.com/vysheng/tg/) 13 | 14 | [![Packagist](https://img.shields.io/packagist/v/zyberspace/telegram-cli-client.svg)](https://packagist.org/packages/zyberspace/telegram-cli-client) 15 | [![Codacy](https://www.codacy.com/project/badge/4175a9bbf88547cdbd94cf57c457068d)](https://www.codacy.com/app/zyberspace/php-telegram-cli-client) 16 | [![License](https://img.shields.io/github/license/zyberspace/php-telegram-cli-client.svg)](https://www.mozilla.org/MPL/2.0/) 17 | 18 | Requirements 19 | ------------ 20 | - a running [telegram-cli](https://github.com/vysheng/tg/) listening on a unix-socket (`-S`) or a port (`-P`) and returning all answers as JSON (`--json`). 21 | Needs to be configured already (phone-number, etc.). 22 | - php >= 5.3.0 23 | 24 | Usage 25 | ----- 26 | 27 | ###Setup telegram-cli 28 | [telegram-cli](https://github.com/vysheng/tg/) needs to run on a unix-socket (`-S`) or a port (`-P`), so *telegram-cli-client* can connect to it. All answers need to be returned as JSON (`--json`). 29 | You should also start it with `-W` so the contact-list gets loaded on startup. 30 | For this example we will take the following command (execute it from the dir, where you installed telegram-cli, not the php-client), `-d` lets it run as daemon.: 31 | 32 | ```shell 33 | ./bin/telegram-cli --json -dWS /tmp/tg.sck & 34 | ``` 35 | 36 | If you run the telegram-cli under another user than your php-script and you are using linux, you need to change the rights of the socket so that the php-script can access it (thanks to [dennydai](https://github.com/dennydai) for this!). 37 | For example, add both to a `telegram`-group and then do 38 | 39 | ```shell 40 | chown :telegram /tmp/tg.sck 41 | chmod g+rwx /tmp/tg.sck 42 | ``` 43 | 44 | If you never started telegram-cli before, you need to start it first in normal mode, so you can type in your telegram-phone-number and register it, if needed (`./bin/telegram-cli`). 45 | 46 | To stop the daemon use `killall telegram-cli` or `kill -TERM [telegram-pid]`. 47 | 48 | ###Install telegram-cli-client with composer 49 | In your project-root: 50 | 51 | ```shell 52 | composer require --update-no-dev zyberspace/telegram-cli-client 53 | ``` 54 | 55 | Composer will then automatically add the package to your project requirements and install it (also creates the `composer.json` if you don't have one already). 56 | If you want to use the discovery-shell, remove the `--update-no-dev` from the command. 57 | 58 | ###Use it 59 | 60 | ```php 61 | require('vendor/autoload.php'); 62 | $telegram = new \Zyberspace\Telegram\Cli\Client('unix:///tmp/tg.sck'); 63 | 64 | $contactList = $telegram->getContactList(); 65 | $telegram->msg($contactList[0]->print_name, 'Hey man, what\'s up? :D'); 66 | ``` 67 | 68 | ###Use it with the discovery-shell 69 | A really easy way to learn the api is by using the embedded [discover-shell](https://github.com/zyberspace/php-discovery-shell). You need to install the dev-dependencies for this (`composer update --dev`). 70 | 71 | ```shell 72 | $ ./discovery-shell.php 73 | -- discovery-shell to help discover a class or library -- 74 | 75 | Use TAB for autocompletion and your arrow-keys to navigate through your method-history. 76 | Beware! This is not a full-featured php-shell. The input gets parsed with PHP-Parser to avoid the usage of 77 | eval(). 78 | > $telegram->getContactList(); 79 | array(13) { 80 | [...] 81 | } 82 | > $telegram->msg('my_contact', 'Hi, i am sending this from a php-client for telegram-cli.'); 83 | true 84 | ``` 85 | 86 | Documentation 87 | ------------- 88 | To create the docs, just run `phpdoc` in the the project-root. 89 | An online-version is available at [phpdoc.zyberware.org/zyberspace/telegram-cli-client](http://phpdoc.zyberware.org/zyberspace/telegram-cli-client/). 90 | 91 | Supported Commands 92 | ------------------ 93 | You can execute every command with the `exec()`-method from the `RawClient`-class, but there are also several command-wrappers available ([doc-link](http://phpdoc.zyberware.org/zyberspace/telegram-cli-client/classes/Zyberspace.Telegram.Cli.Client.html)) that you should prefer to use (see the `example.php`). 94 | If you prefer to only use your own command-wrappers instead, just extend the `RawClient`-class (takes care about the socket-connection and has some helper-methods). 95 | 96 | In your command-wrappers or if you use `exec()` directly, please don't forget to escape the peers (contacts, chat-names, etc.) with `escapePeer()` and all string-arguments with `escapeStringArgument()` to avoid errors. 97 | 98 | API-Stability 99 | ------------- 100 | The api of the commands wrappers will definitely change in the future, for example `getHistory()` only returns the raw answer of the telegram-cli right now and is not really useful. Therefore you should not use this in production yet. 101 | 102 | **If you still want to use this in your project, make sure you stay at the same [minor](http://semver.org/spec/v2.0.0.html)-version (`~0.1.0` or `0.1.*` for example)**. 103 | 104 | License 105 | ------- 106 | This software is licensed under the [Mozilla Public License v. 2.0](http://mozilla.org/MPL/2.0/). For more information, read the file `LICENSE`. 107 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zyberspace/telegram-cli-client", 3 | "description": "php-client for telegram-cli", 4 | "type": "library", 5 | "homepage": "https://github.com/zyberspace/php-telegram-cli-client", 6 | "license": "MPL-2.0", 7 | "authors": [ 8 | { 9 | "name": "zyberspace", 10 | "email": "zyberspace@zyberware.org" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.3.0,<6.0.0" 15 | }, 16 | "require-dev": { 17 | "zyberspace/discovery-shell": "dev-master" 18 | }, 19 | "autoload": { 20 | "psr-0": { 21 | "Zyberspace\\Telegram\\Cli\\": "lib/" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /discovery-shell.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | 5 | * 6 | * This Source Code Form is subject to the terms of the Mozilla Public 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this 8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 | */ 10 | require('vendor/autoload.php'); 11 | 12 | $telegram = new \Zyberspace\Telegram\Cli\Client('unix:///tmp/tg.sck'); 13 | 14 | $discoverShell = new \Zyberspace\DiscoveryShell($telegram, 'telegram', __DIR__ . '/.developer-shell-history'); 15 | $discoverShell->run(); 16 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 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 | require('vendor/autoload.php'); 10 | $telegram = new \Zyberspace\Telegram\Cli\Client('unix:///tmp/tg.sck'); 11 | 12 | $contactList = $telegram->getContactList(); 13 | var_dump($contactList); 14 | 15 | var_dump($telegram->msg($contactList[0]->print_name, '"Te\'st"' . "\n" . time())); 16 | -------------------------------------------------------------------------------- /lib/Zyberspace/Telegram/Cli/Client.php: -------------------------------------------------------------------------------- 1 | 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 | /** 12 | * php-client for telegram-cli. 13 | * If you don't need the command-wrappers in this class or want to make your own, use the RawClient-class. :) 14 | */ 15 | class Client extends RawClient 16 | { 17 | /** 18 | * Sets status as online. 19 | * 20 | * @return boolean true on success, false otherwise 21 | * 22 | * @uses exec() 23 | */ 24 | public function setStatusOnline() 25 | { 26 | return $this->exec('status_online'); 27 | } 28 | 29 | /** 30 | * Sets status as offline. 31 | * 32 | * @return boolean true on success, false otherwise 33 | * 34 | * @uses exec() 35 | */ 36 | public function setStatusOffline() 37 | { 38 | return $this->exec('status_offline'); 39 | } 40 | 41 | /** 42 | * Sends a typing notification to $peer. 43 | * Lasts a couple of seconds or till you send a message (whatever happens first). 44 | * 45 | * @param string $peer The peer, gets escaped with escapePeer() 46 | * 47 | * @return boolean true on success, false otherwise 48 | */ 49 | public function sendTyping($peer) 50 | { 51 | return $this->exec('send_typing ' . $this->escapePeer($peer)); 52 | } 53 | 54 | /** 55 | * Sends a text message to $peer. 56 | * 57 | * @param string $peer The peer, gets escaped with escapePeer() 58 | * @param string $msg The message to send, gets escaped with escapeStringArgument() 59 | * 60 | * @return boolean true on success, false otherwise 61 | * 62 | * @uses exec() 63 | * @uses escapePeer() 64 | * @uses escapeStringArgument() 65 | */ 66 | public function msg($peer, $msg) 67 | { 68 | $peer = $this->escapePeer($peer); 69 | $msg = $this->escapeStringArgument($msg); 70 | return $this->exec('msg ' . $peer . ' ' . $msg); 71 | } 72 | 73 | /** 74 | * Sends a text message to several users at once. 75 | * 76 | * @param array $userList List of users / contacts that shall receive the message, 77 | * gets formated with formatPeerList() 78 | * @param string $msg The message to send, gets escaped with escapeStringArgument() 79 | * 80 | * @return boolean true on success, false otherwise 81 | */ 82 | public function broadcast(array $userList, $msg) 83 | { 84 | return $this->exec('broadcast ' . $this->formatPeerList($userList) . ' ' 85 | . $this->escapeStringArgument($msg)); 86 | } 87 | 88 | /** 89 | * Creates a new group chat with the users in $userList. 90 | * 91 | * @param string $chatTitle The title of the new chat 92 | * @param array $userList The users you want to add to the chat. Gets formatted with formatPeerList(). 93 | * The current telgram-user (who creates the chat) will be added automatically. 94 | * 95 | * @return boolean true on success, false otherwise 96 | * 97 | * @uses exec() 98 | * @uses escapeStringArgument() 99 | * @uses formatPeerList() 100 | */ 101 | public function createGroupChat($chatTitle, $userList) 102 | { 103 | if (count($userList) <= 0) { 104 | return false; 105 | } 106 | 107 | return $this->exec('create_group_chat', $this->escapeStringArgument($chatTitle), 108 | $this->formatPeerList($userList)); 109 | } 110 | 111 | /** 112 | * Returns an info-object about a chat (title, name, members, admin, etc.). 113 | * 114 | * @param string $chat The name of the chat (not the title). Gets escaped with escapePeer(). 115 | * 116 | * @return object|boolean A chat-object; false if somethings goes wrong 117 | * 118 | * @uses exec() 119 | * @uses escapePeer() 120 | */ 121 | public function chatInfo($chat) 122 | { 123 | return $this->exec('chat_info', $this->escapePeer($chat)); 124 | } 125 | 126 | /** 127 | * Renames a chat. Both, the chat title and the print-name will change. 128 | * 129 | * @param string $chat The name of the chat (not the title). Gets escaped with escapePeer(). 130 | * @param string $chatTitle The new title of the chat. 131 | * 132 | * @return boolean true on success, false otherwise 133 | * 134 | * @uses exec() 135 | * @uses escapePeer() 136 | * @uses escapeStringArgument() 137 | */ 138 | public function renameChat($chat, $newChatTitle) 139 | { 140 | return $this->exec('rename_chat', $this->escapePeer($chat), $this->escapeStringArgument($newChatTitle)); 141 | } 142 | 143 | /** 144 | * Adds a user to a chat. 145 | * 146 | * @param string $chat The chat you want the user to add to. Gets escaped with escapePeer(). 147 | * @param string $user The user you want to add. Gets escaped with escapePeer(). 148 | * @param int $numberOfMessagesToFoward The number of last messages of the chat, the new user should see. 149 | * Default is 100. 150 | * 151 | * @return boolean true on success, false otherwise 152 | * 153 | * @uses exec() 154 | * @uses escapePeer() 155 | */ 156 | public function chatAddUser($chat, $user, $numberOfMessagesToFoward = 100) 157 | { 158 | return $this->exec('chat_add_user', $this->escapePeer($chat), $this->escapePeer($user), 159 | (int) $numberOfMessagesToFoward); 160 | } 161 | 162 | /** 163 | * Deletes a user from a chat. 164 | * 165 | * @param string $chat The chat you want the user to delete from. Gets escaped with escapePeer(). 166 | * @param string $user The user you want to delete. Gets escaped with escapePeer(). 167 | * 168 | * @return boolean true on success, false otherwise 169 | * 170 | * @uses exec() 171 | * @uses escapePeer() 172 | */ 173 | public function chatDeleteUser($chat, $user) 174 | { 175 | return $this->exec('chat_del_user', $this->escapePeer($chat), $this->escapePeer($user)); 176 | } 177 | 178 | /** 179 | * Sets the profile name 180 | * 181 | * @param $firstName The first name 182 | * @param $lastName The last name 183 | * 184 | * @return object|boolean Your new user-info as an object; false if somethings goes wrong 185 | * 186 | * @uses exec() 187 | */ 188 | public function setProfileName($firstName, $lastName) 189 | { 190 | return $this->exec('set_profile_name ' . $this->escapeStringArgument($firstName) . ' ' 191 | . $this->escapeStringArgument($lastName)); 192 | } 193 | 194 | /** 195 | * Adds a user to the contact list 196 | * 197 | * @param string $phoneNumber The phone-number of the new contact, needs to be a telegram-user. 198 | * Every char that is not a number gets deleted, so you don't need to care about spaces, 199 | * '+' and so on. 200 | * @param string $firstName The first name of the new contact 201 | * @param string $lastName The last name of the new contact 202 | * 203 | * @return object|boolean The new contact-info as an object; false if somethings goes wrong 204 | * 205 | * @uses exec() 206 | * @uses escapeStringArgument() 207 | */ 208 | public function addContact($phoneNumber, $firstName, $lastName) 209 | { 210 | $phoneNumber = preg_replace('%[^0-9]%', '', (string) $phoneNumber); 211 | if (empty($phoneNumber)) { 212 | return false; 213 | } 214 | 215 | return $this->exec('add_contact ' . $phoneNumber . ' ' . $this->escapeStringArgument($firstName) 216 | . ' ' . $this->escapeStringArgument($lastName)); 217 | } 218 | 219 | /** 220 | * Renames a user in the contact list 221 | * 222 | * @param string $contact The contact, gets escaped with escapePeer() 223 | * @param string $firstName The new first name for the contact 224 | * @param string $lastName The new last name for the contact 225 | * 226 | * @return object|boolean The new contact-info as an object; false if somethings goes wrong 227 | * 228 | * @uses exec() 229 | * @uses escapeStringArgument() 230 | */ 231 | public function renameContact($contact, $firstName, $lastName) 232 | { 233 | return $this->exec('rename_contact ' . $this->escapePeer($contact) 234 | . ' ' . $this->escapeStringArgument($firstName) . ' ' . $this->escapeStringArgument($lastName)); 235 | } 236 | 237 | /** 238 | * Deletes a contact. 239 | * 240 | * @param string $contact The contact, gets escaped with escapePeer() 241 | * 242 | * @return boolean true on success, false otherwise 243 | * 244 | * @uses exec() 245 | * @uses escapePeer() 246 | */ 247 | public function deleteContact($contact) 248 | { 249 | return $this->exec('del_contact ' . $this->escapePeer($contact)); 250 | } 251 | 252 | 253 | /** 254 | * Blocks a user . 255 | * 256 | * @param string $user The user, gets escaped with escapePeer() 257 | * 258 | * @return boolean true on success, false otherwise 259 | * 260 | * @uses exec() 261 | * @uses escapePeer() 262 | */ 263 | public function blockUser($user) 264 | { 265 | return $this->exec('block_user ' . $this->escapePeer($user)); 266 | } 267 | 268 | /** 269 | * Unblocks a user. 270 | * 271 | * @param string $user The user, gets escaped with escapePeer() 272 | * 273 | * @return boolean true on success, false otherwise 274 | * 275 | * @uses exec() 276 | * @uses escapePeer() 277 | */ 278 | public function unblockUser($user) 279 | { 280 | return $this->exec('unblock_user ' . $this->escapePeer($user)); 281 | } 282 | 283 | /** 284 | * Marks all messages with $peer as read. 285 | * 286 | * @param string $peer The peer, gets escaped with escapePeer() 287 | * 288 | * @return boolean true on success, false otherwise 289 | * 290 | * @uses exec() 291 | * @uses escapePeer() 292 | */ 293 | public function markRead($peer) 294 | { 295 | return $this->exec('mark_read ' . $this->escapePeer($peer)); 296 | } 297 | 298 | /** 299 | * Returns an array of all contacts. Every contact is an object like it gets returned from `getUserInfo()`. 300 | * 301 | * @return array|boolean An array with your contacts as objects; false if somethings goes wrong 302 | * 303 | * @uses exec() 304 | * 305 | * @see getUserInfo() 306 | */ 307 | public function getContactList() 308 | { 309 | return $this->exec('contact_list'); 310 | } 311 | 312 | /** 313 | * Returns the informations about the user as an object. 314 | * 315 | * @param string $user The user, gets escaped with escapePeer() 316 | * 317 | * @return object|boolean An object with informations about the user; false if somethings goes wrong 318 | * 319 | * @uses exec() 320 | * @uses escapePeer() 321 | */ 322 | public function getUserInfo($user) 323 | { 324 | return $this->exec('user_info ' . $this->escapePeer($user)); 325 | } 326 | 327 | /** 328 | * Returns an array of all your dialogs. Every dialog is an object with type "user" or "chat". 329 | * 330 | * @return array|boolean An array with your dialogs; false if somethings goes wrong 331 | * 332 | * @uses exec() 333 | * 334 | * @see getUserInfo() 335 | */ 336 | public function getDialogList() 337 | { 338 | return $this->exec('dialog_list'); 339 | } 340 | 341 | /** 342 | * Returns an array of your past message with that $peer. Every message is an object which provides informations 343 | * about it's type, sender, retriever and so one. 344 | * All messages will also be marked as read. 345 | * 346 | * @param string $peer The peer, gets escaped with escapePeer() 347 | * @param int $limit (optional) Limit answer to $limit messages. If not set, there is no limit. 348 | * @param int $offset (optional) Use this with the $limit parameter to go through older messages. 349 | * Can also be negative. 350 | * 351 | * @return array|boolean An array with your past messages with that $peer; false if somethings goes wrong 352 | * 353 | * @uses exec() 354 | * @uses escapePeer() 355 | */ 356 | public function getHistory($peer, $limit = null, $offset = null) 357 | { 358 | if ($limit !== null) { 359 | $limit = (int) $limit; 360 | if ($limit < 1) { //if limit is lesser than 1, telegram-cli crashes 361 | $limit = 1; 362 | } 363 | $limit = ' ' . $limit; 364 | } else { 365 | $limit = ''; 366 | } 367 | if ($offset !== null) { 368 | $offset = ' ' . (int) $offset; 369 | } else { 370 | $offset = ''; 371 | } 372 | 373 | return $this->exec('history ' . $this->escapePeer($peer) . $limit . $offset); 374 | } 375 | 376 | /** 377 | * Send picture to peer 378 | * 379 | * @param string $peer The peer, gets escaped with escapePeer() 380 | * @param string $path The picture path, gets formatted with formatFileName() 381 | * @return boolean 382 | * 383 | * @uses exec() 384 | * @uses escapePeer() 385 | * @uses formatFileName() 386 | */ 387 | public function sendPicture($peer, $path) 388 | { 389 | $peer = $this->escapePeer($peer); 390 | $formattedPath = $this->formatFileName($path); 391 | 392 | return $this->exec('send_photo ' . $peer . ' ' . $formattedPath); 393 | } 394 | 395 | /** 396 | * Send file to peer 397 | * 398 | * @param string $peer The peer, gets escaped with escapePeer() 399 | * @param string $path The file path, gets formatted with formatFileName() 400 | * @return boolean 401 | * 402 | * @uses exec() 403 | * @uses escapePeer() 404 | * @uses formatFileName() 405 | */ 406 | public function sendFile($peer, $path) 407 | { 408 | $peer = $this->escapePeer($peer); 409 | $formattedPath = $this->formatFileName($path); 410 | 411 | return $this->exec('send_file ' . $peer . ' ' . $formattedPath); 412 | } 413 | } 414 | -------------------------------------------------------------------------------- /lib/Zyberspace/Telegram/Cli/ClientException.php: -------------------------------------------------------------------------------- 1 | 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 | /** 12 | * Nothing special here, just an Exception used by the Client. 13 | */ 14 | class ClientException extends \Exception 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /lib/Zyberspace/Telegram/Cli/RawClient.php: -------------------------------------------------------------------------------- 1 | 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 | /** 12 | * Raw part of the php-client for telegram-cli. 13 | * Takes care of the socket-connection and some helper-methods. 14 | */ 15 | class RawClient 16 | { 17 | /** 18 | * The file handler for the socket-connection 19 | * 20 | * @var ressource 21 | */ 22 | protected $_fp; 23 | 24 | /** 25 | * If telegram-cli returns an error, the error-message gets stored here. 26 | * 27 | * @var string 28 | */ 29 | protected $_errorMessage = null; 30 | 31 | /** 32 | * If telegram-cli returns an error, the error-code gets stored here. 33 | * 34 | * @var int 35 | */ 36 | protected $_errorCode = null; 37 | 38 | /** 39 | * Connects to the telegram-cli. 40 | * 41 | * @param string $remoteSocket Address of the socket to connect to. See stream_socket_client() for more info. 42 | * Can be 'unix://' or 'tcp://'. 43 | * 44 | * @throws ClientException Throws an exception if no connection can be established. 45 | */ 46 | public function __construct($remoteSocket) 47 | { 48 | $this->_fp = stream_socket_client($remoteSocket); 49 | if ($this->_fp === false) { 50 | throw new ClientException('Could not connect to socket "' . $remoteSocket . '"'); 51 | } 52 | } 53 | 54 | /** 55 | * Closes the connection to the telegram-cli. 56 | */ 57 | public function __destruct() 58 | { 59 | fclose($this->_fp); 60 | } 61 | 62 | /** 63 | * Executes a command on the telegram-cli. Line-breaks will be escaped, as telgram-cli does not support them. 64 | * 65 | * @param string $command The command. Command-arguments can be passed as additional method-arguments. 66 | * 67 | * @return object|boolean Returns the answer as a json-object or true on success, false if there was an error. 68 | */ 69 | public function exec($command) 70 | { 71 | $command = implode(' ', func_get_args()); 72 | 73 | fwrite($this->_fp, str_replace("\n", '\n', $command) . PHP_EOL); 74 | 75 | $answer = fgets($this->_fp); //"ANSWER $bytes" or false if an error occurred 76 | if (is_string($answer)) { 77 | if (substr($answer, 0, 7) === 'ANSWER ') { 78 | $bytes = ((int) substr($answer, 7)) + 1; //+1 because the json-return seems to miss one byte 79 | if ($bytes > 0) { 80 | $bytesRead = 0; 81 | $jsonString = ''; 82 | 83 | //Run fread() till we have all the bytes we want 84 | //(as fread() can only read a maximum of 8192 bytes from a read-buffered stream at once) 85 | do { 86 | $jsonString .= fread($this->_fp, $bytes - $bytesRead); 87 | $bytesRead = strlen($jsonString); 88 | } while ($bytesRead < $bytes); 89 | 90 | $json = json_decode($jsonString); 91 | 92 | if (!isset($json->error)) { 93 | //Reset error-message and error-code 94 | $this->_errorMessage = null; 95 | $this->_errorCode = null; 96 | 97 | //For "status_online" and "status_offline" 98 | if (isset($json->result) && $json->result === 'SUCCESS') { 99 | return true; 100 | } 101 | 102 | //Return json-object 103 | return $json; 104 | } else { 105 | $this->_errorMessage = $json->error; 106 | $this->_errorCode = $json->error_code; 107 | } 108 | } 109 | } 110 | } 111 | 112 | return false; 113 | } 114 | 115 | /** 116 | * Returns the error-message retrieved vom telegram-cli, if there is one. 117 | * 118 | * @return string|null The error-message retrieved from telegram-cli or null if there was no error. 119 | */ 120 | public function getErrorMessage() 121 | { 122 | return $this->_errorMessage; 123 | } 124 | 125 | /** 126 | * Returns the error-code retrieved vom telegram-cli, if there is one. 127 | * 128 | * @return string|null The error-message retrieved from telegram-cli or null if there was no error. 129 | */ 130 | public function getErrorCode() 131 | { 132 | return $this->_errorCode; 133 | } 134 | 135 | /** 136 | * Escapes strings for usage as command-argument. 137 | * T"es't -> "T\"es\'t" 138 | * 139 | * @param string $argument The argument to escape 140 | * 141 | * @return string The escaped command enclosed by double-quotes 142 | */ 143 | public function escapeStringArgument($argument) 144 | { 145 | return '"' . addslashes($argument) . '"'; 146 | } 147 | 148 | /** 149 | * Replaces all spaces with underscores. 150 | * 151 | * @param string $peer The peer to escape 152 | * 153 | * @return string The escaped peer 154 | */ 155 | public function escapePeer($peer) 156 | { 157 | return str_replace(' ', '_', $peer); 158 | } 159 | 160 | /** 161 | * Takes a list of peers and turns it into a format needed by the most commands that handle multiple peers. 162 | * Every single peer gets escaped by escapePeer(). 163 | * 164 | * @param array $peerList The list of peers that shall get formated 165 | * 166 | * @return string The formated list of peers 167 | * 168 | * @uses escapePeer() 169 | */ 170 | public function formatPeerList(array $peerList) 171 | { 172 | return implode(' ', array_map(array($this, 'escapePeer'), $peerList)); 173 | } 174 | 175 | /** 176 | * Turns the given $fileName into an absolute file path and escapes him 177 | * 178 | * @param string $fileName The path to the file (can be relative or absolute) 179 | * 180 | * @return string The absolute path to the file 181 | */ 182 | public function formatFileName($fileName) 183 | { 184 | return $this->escapeStringArgument(realpath($fileName)); 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /phpdoc.dist.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | zyberspace/telegram-cli-client 4 | 5 | doc 6 | 7 | 8 | doc 9 | 10 | 11 | quiet 12 | 13 | 14 | lib 15 | 16 | 17 | --------------------------------------------------------------------------------