\Zyberspace\Telegram\CliClient

php-client for telegram-cli.

If you don't need the command-wrappers in this class or want to make your own, use the RawClient-class. :)

Summary

Methods
Properties
Constants
__construct()
__destruct()
exec()
getErrorMessage()
getErrorCode()
escapeStringArgument()
escapePeer()
formatPeerList()
formatFileName()
setStatusOnline()
setStatusOffline()
sendTyping()
msg()
broadcast()
createGroupChat()
chatInfo()
renameChat()
chatAddUser()
chatDeleteUser()
setProfileName()
addContact()
renameContact()
deleteContact()
blockUser()
unblockUser()
markRead()
getContactList()
getUserInfo()
getDialogList()
getHistory()
No public properties found
No constants found
No protected methods found
$_fp
$_errorMessage
$_errorCode
N/A
No private methods found
No private properties found
N/A

Properties

$_fp

$_fp : \Zyberspace\Telegram\Cli\ressource

The file handler for the socket-connection

Type

\Zyberspace\Telegram\Cli\ressource

$_errorMessage

$_errorMessage : string

If telegram-cli returns an error, the error-message gets stored here.

Type

string

$_errorCode

$_errorCode : integer

If telegram-cli returns an error, the error-code gets stored here.

Type

integer

Methods

__construct()

__construct(string  $remoteSocket) 

Connects to the telegram-cli.

Parameters

string $remoteSocket

Address of the socket to connect to. See stream_socket_client() for more info. Can be 'unix://' or 'tcp://'.

Throws

\Zyberspace\Telegram\Cli\ClientException

Throws an exception if no connection can be established.

__destruct()

__destruct() 

Closes the connection to the telegram-cli.

exec()

exec(string  $command) : object|boolean

Executes a command on the telegram-cli. Line-breaks will be escaped, as telgram-cli does not support them.

Parameters

string $command

The command. Command-arguments can be passed as additional method-arguments.

Returns

object|boolean —

Returns the answer as a json-object or true on success, false if there was an error.

getErrorMessage()

getErrorMessage() : string|null

Returns the error-message retrieved vom telegram-cli, if there is one.

Returns

string|null —

The error-message retrieved from telegram-cli or null if there was no error.

getErrorCode()

getErrorCode() : string|null

Returns the error-code retrieved vom telegram-cli, if there is one.

Returns

string|null —

The error-message retrieved from telegram-cli or null if there was no error.

escapeStringArgument()

escapeStringArgument(string  $argument) : string

Escapes strings for usage as command-argument.

T"es't -> "T\"es\'t"

Parameters

string $argument

The argument to escape

Returns

string —

The escaped command enclosed by double-quotes

escapePeer()

escapePeer(string  $peer) : string

Replaces all spaces with underscores.

Parameters

string $peer

The peer to escape

Returns

string —

The escaped peer

formatPeerList()

formatPeerList(array  $peerList) : string

Takes a list of peers and turns it into a format needed by the most commands that handle multiple peers.

Every single peer gets escaped by escapePeer().

Parameters

array $peerList

The list of peers that shall get formated

Returns

string —

The formated list of peers

formatFileName()

formatFileName(string  $fileName) : string

Turns the given $fileName into an absolute file path and escapes him

Parameters

string $fileName

The path to the file (can be relative or absolute)

Returns

string —

The absolute path to the file

setStatusOnline()

setStatusOnline() : boolean

Sets status as online.

Returns

boolean —

true on success, false otherwise

setStatusOffline()

setStatusOffline() : boolean

Sets status as offline.

Returns

boolean —

true on success, false otherwise

sendTyping()

sendTyping(string  $peer) : boolean

Sends a typing notification to $peer.

Lasts a couple of seconds or till you send a message (whatever happens first).

Parameters

string $peer

The peer, gets escaped with escapePeer()

Returns

boolean —

true on success, false otherwise

msg()

msg(string  $peer, string  $msg) : boolean

Sends a text message to $peer.

Parameters

string $peer

The peer, gets escaped with escapePeer()

string $msg

The message to send, gets escaped with escapeStringArgument()

Returns

boolean —

true on success, false otherwise

broadcast()

broadcast(array  $userList, string  $msg) : boolean

Sends a text message to several users at once.

Parameters

array $userList

List of users / contacts that shall receive the message, gets formated with formatPeerList()

string $msg

The message to send, gets escaped with escapeStringArgument()

Returns

boolean —

true on success, false otherwise

createGroupChat()

createGroupChat(string  $chatTitle, array  $userList) : boolean

Creates a new group chat with the users in $userList.

Parameters

string $chatTitle

The title of the new chat

array $userList

The users you want to add to the chat. Gets formatted with formatPeerList(). The current telgram-user (who creates the chat) will be added automatically.

Returns

boolean —

true on success, false otherwise

chatInfo()

chatInfo(string  $chat) : object|boolean

Returns an info-object about a chat (title, name, members, admin, etc.).

Parameters

string $chat

The name of the chat (not the title). Gets escaped with escapePeer().

Returns

object|boolean —

A chat-object; false if somethings goes wrong

renameChat()

renameChat(string  $chat,   $newChatTitle) : boolean

Renames a chat. Both, the chat title and the print-name will change.

Parameters

string $chat

The name of the chat (not the title). Gets escaped with escapePeer().

$newChatTitle

Returns

boolean —

true on success, false otherwise

chatAddUser()

chatAddUser(string  $chat, string  $user, integer  $numberOfMessagesToFoward = 100) : boolean

Adds a user to a chat.

Parameters

string $chat

The chat you want the user to add to. Gets escaped with escapePeer().

string $user

The user you want to add. Gets escaped with escapePeer().

integer $numberOfMessagesToFoward

The number of last messages of the chat, the new user should see. Default is 100.

Returns

boolean —

true on success, false otherwise

chatDeleteUser()

chatDeleteUser(string  $chat, string  $user) : boolean

Deletes a user from a chat.

Parameters

string $chat

The chat you want the user to delete from. Gets escaped with escapePeer().

string $user

The user you want to delete. Gets escaped with escapePeer().

Returns

boolean —

true on success, false otherwise

setProfileName()

setProfileName(  $firstName,   $lastName) : object|boolean

Sets the profile name

Parameters

$firstName

The first name

$lastName

The last name

Returns

object|boolean —

Your new user-info as an object; false if somethings goes wrong

addContact()

addContact(string  $phoneNumber, string  $firstName, string  $lastName) : object|boolean

Adds a user to the contact list

Parameters

string $phoneNumber

The phone-number of the new contact, needs to be a telegram-user. Every char that is not a number gets deleted, so you don't need to care about spaces, '+' and so on.

string $firstName

The first name of the new contact

string $lastName

The last name of the new contact

Returns

object|boolean —

The new contact-info as an object; false if somethings goes wrong

renameContact()

renameContact(string  $contact, string  $firstName, string  $lastName) : object|boolean

Renames a user in the contact list

Parameters

string $contact

The contact, gets escaped with escapePeer()

string $firstName

The new first name for the contact

string $lastName

The new last name for the contact

Returns

object|boolean —

The new contact-info as an object; false if somethings goes wrong

deleteContact()

deleteContact(string  $contact) : boolean

Deletes a contact.

Parameters

string $contact

The contact, gets escaped with escapePeer()

Returns

boolean —

true on success, false otherwise

blockUser()

blockUser(string  $user) : boolean

Blocks a user .

Parameters

string $user

The user, gets escaped with escapePeer()

Returns

boolean —

true on success, false otherwise

unblockUser()

unblockUser(string  $user) : boolean

Unblocks a user.

Parameters

string $user

The user, gets escaped with escapePeer()

Returns

boolean —

true on success, false otherwise

markRead()

markRead(string  $peer) : boolean

Marks all messages with $peer as read.

Parameters

string $peer

The peer, gets escaped with escapePeer()

Returns

boolean —

true on success, false otherwise

getContactList()

getContactList() : array|boolean

Returns an array of all contacts. Every contact is an object like it gets returned from `getUserInfo()`.

Returns

array|boolean —

An array with your contacts as objects; false if somethings goes wrong

getUserInfo()

getUserInfo(string  $user) : object|boolean

Returns the informations about the user as an object.

Parameters

string $user

The user, gets escaped with escapePeer()

Returns

object|boolean —

An object with informations about the user; false if somethings goes wrong

getDialogList()

getDialogList() : array|boolean

Returns an array of all your dialogs. Every dialog is an object with type "user" or "chat".

Returns

array|boolean —

An array with your dialogs; false if somethings goes wrong

getHistory()

getHistory(string  $peer, integer  $limit = null, integer  $offset = null) : array|boolean

Returns an array of your past message with that $peer. Every message is an object which provides informations about it's type, sender, retriever and so one.

All messages will also be marked as read.

Parameters

string $peer

The peer, gets escaped with escapePeer()

integer $limit

(optional) Limit answer to $limit messages. If not set, there is no limit.

integer $offset

(optional) Use this with the $limit parameter to go through older messages. Can also be negative.

Returns

array|boolean —

An array with your past messages with that $peer; false if somethings goes wrong