Skip to content
This repository was archived by the owner on May 14, 2022. It is now read-only.

Commit 3d5d0a4

Browse files
committed
Initial commit
0 parents  commit 3d5d0a4

File tree

9 files changed

+828
-0
lines changed

9 files changed

+828
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/doc/
3+
/.project

LICENSE

Lines changed: 373 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
zyberspace/telegram-cli-client
2+
==============================
3+
php-client for [telegram-cli](https://github.com/vysheng/tg/)
4+
5+
Requirements
6+
------------
7+
- a running [telegram-cli](https://github.com/vysheng/tg/) listening on a unix-socket (`-S`) or a port (`-P`). Needs to be configured already (phone-number, etc.).
8+
- php >= 5.4.0
9+
10+
Usage
11+
-----
12+
13+
###Setup telegram-cli
14+
[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.
15+
You should also start it with `-W` so the contact-list gets loaded on startup.
16+
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.:
17+
18+
```shell
19+
./bin/telegram-cli -dWS /tmp/tg.sck &
20+
```
21+
22+
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`).
23+
24+
To stop the daemon use `killall telegram-cli` or `kill -TERM [telegram-pid]`.
25+
26+
###Install telegram-cli-client with composer
27+
In your project-root:
28+
29+
```shell
30+
composer require zyberspace/telegram-cli-client
31+
```
32+
33+
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).
34+
35+
###Use it
36+
37+
```php
38+
require('vendor/autoload.php');
39+
$telegram = new \Zyberspace\Telegram\Cli\Client('unix:///tmp/tg.sck');
40+
41+
$contactList = $telegram->getContactList();
42+
$telegram->msg($contactList[0], 'Hey man, what\'s up? :D');
43+
```
44+
45+
Documentation
46+
-------------
47+
To create the docs, just run `phpdoc` in the the project-root.
48+
An online-version is available at [phpdoc.zyberware.org/zyberspace/telegram-cli-client](http://phpdoc.zyberware.org/zyberspace/telegram-cli-client/).
49+
50+
Supported Commands
51+
------------------
52+
You can execute every command with the `exec()`-method, but there are also several command-wrappers available in the `AbstractClientCommands`-Class ([doc-link](http://phpdoc.zyberware.org/zyberspace/telegram-cli-client/classes/Zyberspace.Telegram.Cli.AbstractClientCommands.html)), which the main class extends (see the `example.php`).
53+
54+
If you use `exec()` directly, please don't forget to escape the peer (contacts, chat-names, etc.) with `escapePeer()` and all string-arguments with `escapeStringArgument()` to avoid errors.
55+
56+
API-Stability
57+
-------------
58+
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.
59+
60+
**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)**.
61+
62+
License
63+
-------
64+
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`.

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.4.0,<6.0.0"
15+
},
16+
"autoload": {
17+
"psr-0": {
18+
"Zyberspace\\Telegram\\Cli\\": "lib/"
19+
}
20+
}
21+
}

example.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
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], '"Te\'st"' . "\n" . time()));
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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

Comments
 (0)