Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
61481e19
Commit
61481e19
authored
Feb 11, 2016
by
Marco Boretto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #99 from noplanman/correct_readme_and_examples
Correct readme and examples
parents
f11c153c
a6d527f2
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
268 additions
and
206 deletions
+268
-206
README.md
README.md
+189
-128
getUpdatesCLI.php
examples/getUpdatesCLI.php
+32
-33
hook.php
examples/hook.php
+30
-30
set.php
examples/set.php
+11
-9
unset.php
examples/unset.php
+6
-6
No files found.
README.md
View file @
61481e19
...
@@ -12,29 +12,36 @@
...
@@ -12,29 +12,36 @@
A Telegram Bot based on the official
[
Telegram Bot API
](
https://core.telegram.org/bots/api
)
A Telegram Bot based on the official
[
Telegram Bot API
](
https://core.telegram.org/bots/api
)
### Introduction
## Introduction
This is a pure php Telegram Bot, fully extensible via plugins.
This is a pure PHP Telegram Bot, fully extensible via plugins.
Telegram recently announced official support for a
[
Bot
Telegram recently announced official support for a
[
Bot
API](https://telegram.org/blog/bot-revolution) allowing integrators of
API](https://telegram.org/blog/bot-revolution) allowing integrators of
all sorts to bring automated interactions to the mobile platform. This
all sorts to bring automated interactions to the mobile platform. This
Bot aims to provide a platform where one c
ould
simply write a plugin
Bot aims to provide a platform where one c
an
simply write a plugin
and have interactions in a matter of minutes.
and have interactions in a matter of minutes.
The Bot can:
The Bot can:
-
retri
ve update
with webhook and getUpdate methods.
-
retri
eve updates
with webhook and getUpdate methods.
-
supports all types and methods according to Telegram API (20 January 2016).
-
supports all types and methods according to Telegram API (20 January 2016).
-
supports supergroups.
-
supports supergroups.
-
handle commands in chat with other bots.
-
handle commands in chat with other bots.
-
manage Channel from the bot admin interface
-
manage Channel from the bot admin interface.
-
Full support for
**inline bots**
(
**new!**
)
-
full support for
**inline bots**
. (
**new!**
)
-
Messages, InlineQuery and ChosenInlineQuery are stored in the Database (
**new!**
)
-
Messages, InlineQuery and ChosenInlineQuery are stored in the Database. (
**new!**
)
-----
This code is available on
[
Github
](
https://github.com/akalongman/php-telegram-bot
)
. Pull requests are welcome.
## Instructions
## Instructions
### Create your first bot
### Create your first bot
1.
Message @botfather https://telegram.me/botfather with the following
1.
Message @botfather https://telegram.me/botfather with the following
text:
`/newbot`
text:
`/newbot`
If you don't know how to message by username, click the search
If you don't know how to message by username, click the search
field on your Telegram app and type
`@botfather`
, you should be able
field on your Telegram app and type
`@botfather`
,
where
you should be able
to initiate a conversation. Be careful not to send it to the wrong
to initiate a conversation. Be careful not to send it to the wrong
contact, because some users has similar usernames to
`botfather`
.
contact, because some users has similar usernames to
`botfather`
.
...
@@ -45,25 +52,27 @@ call it? Please choose a name for your bot.`
...
@@ -45,25 +52,27 @@ call it? Please choose a name for your bot.`
3.
Type whatever name you want for your bot.
3.
Type whatever name you want for your bot.
4.
@botfather replies with
`Good. Now let's choose a username for your
4.
@botfather replies with
`
``
Good. Now let's choose a username for your
bot. It must end in `bot`. Like this, for example: TetrisBot or
bot. It must end in `bot`. Like this, for example: TetrisBot or
tetris_bot.`
tetris_bot.`
``
5.
Type whatever username you want for your bot, minimum 5 characters,
5.
Type whatever username you want for your bot, minimum 5 characters,
and must end with
`bot`
. For example:
`telesample_bot`
and must end with
`bot`
. For example:
`telesample_bot`
6.
@botfather replies with:
6.
@botfather replies with:
```
Done! Congratulations on your new bot. You will find it at
Done! Congratulations on your new bot. You will find it at
telegram.me/telesample_bot. You can now add a description, about
telegram.me/telesample_bot. You can now add a description, about
section and profile picture for your bot, see /help for a list of
section and profile picture for your bot, see /help for a list of
commands.
commands.
Use this token to access the HTTP API:
Use this token to access the HTTP API:
<b>123456789:AAG90e14-0f8-40183D-18491dDE</b>
123456789:AAG90e14-0f8-40183D-18491dDE
For a description of the Bot API, see this page:
For a description of the Bot API, see this page:
https://core.telegram.org/bots/api
https://core.telegram.org/bots/api
```
7.
Note down the 'token' mentioned above.
7.
Note down the 'token' mentioned above.
...
@@ -73,28 +82,29 @@ https://core.telegram.org/bots/api
...
@@ -73,28 +82,29 @@ https://core.telegram.org/bots/api
9.
@botfather replies with
`Choose a bot to change group messages settings.`
9.
@botfather replies with
`Choose a bot to change group messages settings.`
10.
Type
`@telesample_bot`
(change to the username you set at step 5
10.
Type
(or select)
`@telesample_bot`
(change to the username you set at step 5
above, but start it with
`@`
)
above, but start it with
`@`
)
11.
@botfather replies with
11.
@botfather replies with
'Enable' - your bot will only receive messages that either start
```
with the '/' symbol or mention the bot by username.
'Enable' - your bot will only receive messages that either start
with the '/' symbol or mention the bot by username.
'Disable' - your bot will receive all messages that people send to groups.
'Disable' - your bot will receive all messages that people send to groups.
Current status is: ENABLED
Current status is: ENABLED
```
12.
Type
`Disable`
to let your bot receive all messages sent to a
12.
Type
(or select)
`Disable`
to let your bot receive all messages sent to a
group. This step is up to you actually.
group. This step is up to you actually.
13.
@botfather replies with
`Success! The new status is: DISABLED. /help`
13.
@botfather replies with
`Success! The new status is: DISABLED. /help`
### Require this package with Composer
### Require this package with Composer
Install this package through
[
Composer
](
https://getcomposer.org/
)
.
Install this package through
[
Composer
](
https://getcomposer.org/
)
.
Edit your project's
`composer.json`
file to require
Edit your project's
`composer.json`
file to require
`longman/telegram-bot`
.
`longman/telegram-bot`
.
Create
*composer.json*
file
:
Create
*composer.json*
file
```
js
```
js
{
{
"
name
"
:
"
yourproject/yourproject
"
,
"
name
"
:
"
yourproject/yourproject
"
,
...
@@ -105,40 +115,47 @@ Create *composer.json* file:
...
@@ -105,40 +115,47 @@ Create *composer.json* file:
}
}
}
}
```
```
And run composer update
and run
`composer update`
**Or**
run a command in your command line:
**or**
run this command in your command line:
```
```
composer require longman/telegram-bot
composer require longman/telegram-bot
```
```
### Choose how to retrieve Telegram updates
### Choose how to retrieve Telegram updates
The bot can handle updates with
**webhook**
or
**getUpdate**
method:
The bot can handle updates with
**webhook**
or
**getUpdate**
method:
| | Webhook | getUpdate |
| | Webhook | getUpdate |
| ---- | :----: | :----: |
| ---- | :----: | :----: |
| Description | Telegram send
the update directy to your host | You have to fetch Telegram updates
|
| Description | Telegram send
s the updates directly to your host | You have to fetch Telegram updates manually
|
| Host with https | Required | Not required |
| Host with https | Required | Not required |
| Mysql | Not required | Required |
| MySQL | Not required | Required |
## Webhook installation
## Webhook installation
You need server with https and composer support.
You must set a
[
WebHook
](
https://core.telegram.org/bots/api#setwebhook
)
.
In order to set a
[
Webhook
](
https://core.telegram.org/bots/api#setwebhook
)
you need a server with https and composer support.
Create
*set.php*
(just copy and edit
*examples/set.php*
) and put into it:
(For a
[
self signed certificate
](
#self-signed-certificate
)
you need to add some extra code)
Create
*set.php*
(or just copy and edit
*examples/set.php*
) and put into it:
```
php
```
php
<?php
<?php
//
Composer Load
er
//
Load compos
er
$loader
=
require
__DIR__
.
'/vendor/autoload.php'
;
require
__DIR__
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'namebot'
;
$BOT_NAME
=
'namebot'
;
$
link
=
'https://yourdomain/yourpath_to_
hook.php'
;
$
hook_url
=
'https://yourdomain/path/to/
hook.php'
;
try
{
try
{
//
c
reate Telegram API object
//
C
reate Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
// set webhook
$result
=
$telegram
->
setWebHook
(
$link
);
// Set webhook
$result
=
$telegram
->
setWebHook
(
$hook_url
);
if
(
$result
->
isOk
())
{
if
(
$result
->
isOk
())
{
echo
$result
->
getDescription
();
echo
$result
->
getDescription
();
}
}
...
@@ -147,52 +164,67 @@ try {
...
@@ -147,52 +164,67 @@ try {
}
}
```
```
And open your
*set.php*
via browser
.
Open your
*set.php*
via the browser to register the webhook with Telegram
.
After, create
*hook.php*
(or just copy and edit
*examples/hook.php*
) and pu
t:
Now, create
*hook.php*
(or just copy and edit
*examples/hook.php*
) and put into i
t:
```
php
```
php
<?php
<?php
$loader
=
require
__DIR__
.
'/vendor/autoload.php'
;
// Load composer
require
__DIR__
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'namebot'
;
$BOT_NAME
=
'namebot'
;
try
{
try
{
//
c
reate Telegram API object
//
C
reate Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
//
h
andle telegram webhook request
//
H
andle telegram webhook request
$telegram
->
handle
();
$telegram
->
handle
();
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
// Silence is golden!
// log telegram errors
// log telegram errors
// echo $e;
// echo $e;
}
}
```
```
###Self Signed Certificate
### Self Signed Certificate
To upload the certificate, add the certificate path as param in
*set.php*
:
To upload the certificate, add the certificate path as a parameter in
*set.php*
:
```
php
```
php
$result
=
$telegram
->
setWebHook
(
$url
,
$certificate_path
);
$result
=
$telegram
->
setWebHook
(
$
hook_
url
,
$certificate_path
);
```
```
### Unset Webhook
Edit
*example/unset.php*
with your bot credentials and execute it.
## getUpdate installation
## getUpdate installation
You need the database Mysql active.
Create
*getUpdateCLI.php*
(just copy and edit
*examples/getUpdateCLI.php*
) and put into it:
The MySQL database must be active!
Create
*getUpdateCLI.php*
(or just copy and edit
*examples/getUpdateCLI.php*
) and put into it:
```
php
```
php
#!/usr/bin/env php
#!/usr/bin/env php
<?php
<?php
$loader
=
require
__DIR__
.
'/vendor/autoload.php'
;
require
__DIR__
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'namebot'
;
$BOT_NAME
=
'namebot'
;
$credentials
=
array
(
'host'
=>
'localhost'
,
'user'
=>
'dbuser'
,
'password'
=>
'dbpass'
,
'database'
=>
'dbname'
);
$mysql_credentials
=
[
'host'
=>
'localhost'
,
'user'
=>
'dbuser'
,
'password'
=>
'dbpass'
,
'database'
=>
'dbname'
,
];
try
{
try
{
//
c
reate Telegram API object
//
C
reate Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
->
enableMySQL
(
$credentials
);
// handle telegram getUpdate request
// Enable MySQL
$telegram
->
enableMySQL
(
$mysql_credentials
);
// Handle telegram getUpdate request
$telegram
->
handleGetUpdates
();
$telegram
->
handleGetUpdates
();
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
// log telegram errors
// log telegram errors
...
@@ -200,7 +232,7 @@ try {
...
@@ -200,7 +232,7 @@ try {
}
}
```
```
give t
o the file the permission for execution
:
give t
he file permission to execute
:
```
```
chmod 775 getUpdateCLI.php
chmod 775 getUpdateCLI.php
```
```
...
@@ -208,91 +240,119 @@ then run
...
@@ -208,91 +240,119 @@ then run
```
```
./getUpdateCLI.php
./getUpdateCLI.php
```
```
## Support
### Types
### Types
All types implemented according to Telegram API (20 January 2016).
#### Inline Query
All types are implemented according to Telegram API (20 January 2016).
### Inline Query
Full support for inline query according to Telegram API (20 January 2016).
Full support for inline query according to Telegram API (20 January 2016).
### Methods
### Methods
All methods implemented according to Telegram API (20 January 2016).
####Send Message
All methods are implemented according to Telegram API (20 January 2016).
Message longer than 4096 characters are splitted in more messages.
#### Send Message
Messages longer than 4096 characters are split up into multiple messages.
```
php
```
php
$result
=
Request
::
sendMessage
([
'chat_id'
=>
$chat_id
,
'text'
=>
'Your utf8 text 😜 ...'
]);
$result
=
Request
::
sendMessage
([
'chat_id'
=>
$chat_id
,
'text'
=>
'Your utf8 text 😜 ...'
]);
```
```
####Send Photo
#### Send Photo
To send a local photo provide the file path as second param:
To send a local photo, provide the file path as the second parameter:
```
php
```
php
$data
[
'chat_id'
]
=
$chat_id
;
$data
=
[
'chat_id'
=>
$chat_id
]
;
$result
=
Request
::
sendPhoto
(
$data
,
$this
->
telegram
->
getUploadPath
()
.
'/'
.
'
image.jpg'
);
$result
=
Request
::
sendPhoto
(
$data
,
$telegram
->
getUploadPath
()
.
'/
image.jpg'
);
```
```
If you know the
file_id of a previously uploaded file, just provide it in the fist param
:
If you know the
`file_id`
of a previously uploaded file, just include it in the data array
:
```
php
```
php
$data
[
'chat_id'
]
=
$chat_id
;
$data
=
[
$data
[
'photo'
]
=
$file_id
;
'chat_id'
=>
$chat_id
,
'photo'
=>
$file_id
,
];
$result
=
Request
::
sendPhoto
(
$data
);
$result
=
Request
::
sendPhoto
(
$data
);
```
```
*sendAudio*
,
*sendDocument*
,
*sendSticker*
,
*sendVideo*
and
*sendVoice*
works in the same way.
*sendAudio*
,
*sendDocument*
,
*sendSticker*
,
*sendVideo*
and
*sendVoice*
all work in the same way.
See
*ImageCommand.php*
for a full example.
See
*examples/Commands/ImageCommand.php*
for a full example.
####Send Chat Action
#### Send Chat Action
```
php
```
php
Request
::
sendChatAction
([
'chat_id'
=>
$chat_id
,
'action'
=>
'typing'
]);
Request
::
sendChatAction
([
'chat_id'
=>
$chat_id
,
'action'
=>
'typing'
]);
```
```
####getUserProfilePhoto
Retrieve the user photo, see the
*WhoamiCommand.php*
for a full example.
####GetFile and dowloadFile
#### getUserProfilePhoto
Get the file path and download it, see the
*WhoamiCommand.php*
for a full example.
Retrieve the user photo, see
*src/Commands/WhoamiCommand.php*
for a full example.
#### getFile and dowloadFile
Get the file path and download it, see
*src/Commands/WhoamiCommand.php*
for a full example.
#### Send message to all active chats
#### Send message to all active chats
To do this you have to enable the Mysql connection.
To do this you have to enable the MySQL connection.
Here's an example of use:
Here's an example of use:
```
php
```
php
$results
=
$telegram
->
sendToActiveChats
(
$results
=
Request
::
sendToActiveChats
(
'sendMessage'
,
//callback function to execute (see Request.php methods)
'sendMessage'
,
// callback function to execute (see Request.php for available methods)
array
(
'text'
=>
'Hey! Checkout the new feature!!'
),
//Param to evaluate the request
[
'text'
=>
'Hey! Check out the new features!!'
],
// Data to pass to the request
true
,
//Send to chats (group chat)
true
,
// Send to chats (group chat)
true
,
//Send to users (single chat)
true
,
// Send to chats (super group chat)
null
,
//'yyyy-mm-dd hh:mm:ss' date range from
true
,
// Send to users (single chat)
null
//'yyyy-mm-dd hh:mm:ss' date range to
null
,
// 'yyyy-mm-dd hh:mm:ss' date range from
null
,
// 'yyyy-mm-dd hh:mm:ss' date range to
);
);
```
```
You can also broadcast message to users, from the private chat with your bot. Take a look at the admin interace below.
## Utilis
### MySQL storage (Recomended)
If you want insert in database messages/users/chats for further usage
in commands, create database and import
*structure.sql*
and enable
Mysql support after object creation and BEFORE handle method:
```
php
You can also broadcast a message to users, from the private chat with your bot. Take a look at the
[
admin commands
](
#admin-commands
)
below.
$credentials
=
[
'host'
=>
'localhost'
,
'user'
=>
'dbuser'
,
'password'
=>
'dbpass'
,
'database'
=>
'dbname'
];
$telegram
->
enableMySQL
(
$credentials
);
## Utils
### MySQL storage (Recommended)
If you want to save messages/users/chats for further usage
in commands, create a new database, import
*structure.sql*
and enable
MySQL support after object creation and BEFORE handle method:
```
php
$mysql_credentials
=
[
'host'
=>
'localhost'
,
'user'
=>
'dbuser'
,
'password'
=>
'dbpass'
,
'database'
=>
'dbname'
,
];
$telegram
->
enableMySQL
(
$mysql_credentials
);
```
```
You can set a custom prefix to all the tables while you are enabling My
sql
:
You can set a custom prefix to all the tables while you are enabling My
SQL
:
```
php
```
php
$telegram
->
enableMySQL
(
$
credentials
,
$BOT_NAME
.
'_'
);
$telegram
->
enableMySQL
(
$
mysql_credentials
,
$BOT_NAME
.
'_'
);
```
```
Consider to use the
*utf8mb4*
branch if you find some special characters problems.
Consider to use the
*utf8mb4*
branch if you find some special characters problems.
You can also store inline query and chosen inline query in the database.
You can also store inline query and chosen inline query in the database.
### Channels Support
### Channels Support
All methods implemented can be used to manage channels.
All methods implemented can be used to manage channels.
With
admin interface
you can manage your channel directly with your bot private chat.
With
[
admin commands
](
#admin-commands
)
you can manage your channel directly with your bot private chat.
### Commands
### Commands
The bot is able to recognise commands in chat with multiple bot (/command@mybot ).
The bot is able to recognise commands in a chat with multiple bots (/command@mybot).
It can execute command triggering a chat event. Here's the list:
It can execute command triggering a chat event. Here's the list:
-
New chat participant (
**NewchatparticipantCommand.php**
)
-
New chat participant (
**NewchatparticipantCommand.php**
)
...
@@ -305,90 +365,92 @@ It can execute command triggering a chat event. Here's the list:
...
@@ -305,90 +365,92 @@ It can execute command triggering a chat event. Here's the list:
-
Inline query (
**InlinequeryCommand.php**
)
-
Inline query (
**InlinequeryCommand.php**
)
-
Chosen inline result (
**ChoseninlineresultCommand.php**
)
-
Chosen inline result (
**ChoseninlineresultCommand.php**
)
**GenericCommand.php**
lets you handle commands that don't exist or to
**GenericCommand.php**
let you handle commands that don't exist or to
use commands as a variable:
use commands as a variable:
Favourite colour?
**/black, /red**
Favourite colour?
**/black, /red**
Favourite number?
**/1, /134**
Favourite number?
**/1, /134**
**GenericmessageCommand.php**
let you handle any type of message.
**GenericmessageCommand.php**
let
s
you handle any type of message.
Maybe you would like to develop your own commands. A good practice is
Maybe you would like to develop your own commands. A good practice is
to store them outside
*vendor/*
. This can be done
adding the method
:
to store them outside
*vendor/*
. This can be done
using
:
```
php
```
php
$
COMMANDS_FOLDER
=
__DIR__
.
'/Commands/'
;
$
commands_folder
=
__DIR__
.
'/Commands/'
;
$telegram
->
addCommandsPath
(
$
COMMANDS_FOLDER
);
$telegram
->
addCommandsPath
(
$
commands_folder
);
```
```
Inside
*examples/Commands/*
there are some sample that show how to use types.
Inside
*examples/Commands/*
there are some sample
s
that show how to use types.
#### Commands Configuration
#### Commands Configuration
With this method you can set some command specified parameters, for
With this method you can set some command specific parameters, for
example, google geocode/timezone api key for date command:
example, google geocode/timezone api key for date command:
```
php
```
php
$telegram
->
setCommandConfig
(
'date'
,
[
'google_api_key'
=>
'your_google_api_key_here'
]);
$telegram
->
setCommandConfig
(
'date'
,
[
'google_api_key'
=>
'your_google_api_key_here'
]);
```
```
### Admin Commands
### Admin Commands
Enabling this feature, the admin bot can perform some super user command like:
Enabling this feature, the admin bot can perform some super user commands like:
-
Send message to all chats
*/sendtoall*
-
Send message to all chats
*/sendtoall*
-
List all the chats started with the bot
*/chats*
-
List all the chats started with the bot
*/chats*
-
Send a message to a channel
*/sendtochannel*
(NEW! see below how to configure it)
-
Send a message to a channel
*/sendtochannel*
(NEW! see below how to configure it)
You can specify one or more admin with this option:
You can specify one or more admin
s
with this option:
```
php
```
php
$telegram
->
enableAdmins
([
'your_telegram_user_id'
,
'Others
id'
]);
$telegram
->
enableAdmins
([
'your_telegram_user_id'
,
'other_telegram_user_
id'
]);
```
```
Telegram user id can be retrieved with the command
**/whoami**
.
Telegram user id can be retrieved with the command
**/whoami**
.
Admin commands are stored in
*src/Admin/*
folder.
Admin commands are stored in
*src/Admin/*
folder.
To
know all the commands avaiable
type
**/help**
.
To
get a list of all available commands,
type
**/help**
.
#### Channel Administration
#### Channel Administration
To enable this feature follow those steps:
To enable this feature follow these steps:
-
Add your bot as channel administrator, this can be done with any telegram client.
-
Add your bot as channel administrator, this can be done with any telegram client.
-
Enable admin interface for your user as explained in the admin section above.
-
Enable admin interface for your user as explained in the admin section above.
-
Enter your channel name as a param
for the sendtoall
command:
-
Enter your channel name as a param
eter for the
*/sendtochannel*
command:
```
php
```
php
$telegram
->
setCommandConfig
(
'sendtochannel'
,
[
'your_channel'
=>
'@type_here_your_channel'
]);
$telegram
->
setCommandConfig
(
'sendtochannel'
,
[
'your_channel'
=>
'@type_here_your_channel'
]);
```
```
-
Enjoy!
-
Enjoy!
### Upload and Download directory path
### Upload and Download directory path
You can overwrite the default Upload and Download directory with:
You can override the default Upload and Download directory with:
```
php
```
php
$telegram
->
setDownloadPath
(
"yourpath/Download"
);
$telegram
->
setDownloadPath
(
'yourpath/Download'
);
$telegram
->
setUploadPath
(
"yourpath../Upload"
);
$telegram
->
setUploadPath
(
'yourpath/Upload'
);
```
```
###Unset Webhook
Edit
*example/unset.php*
with your credential and execute it.
### Logging
### Logging
Thrown Exception are stored in
*TelegramException.log*
file (in the base directory).
Incoming update (json string from webhook and getUpdates) can be logged on a text file, set those options with the methods:
Thrown Exceptions are stored in
*TelegramException.log*
file (in the base directory).
Incoming update (json string from webhook and getUpdates) can be logged in a text file. Set those options with the methods:
```
php
```
php
$telegram
->
setLogRequests
(
true
);
$telegram
->
setLogRequests
(
true
);
$telegram
->
setLogPath
(
$BOT_NAME
.
'.log'
);
$telegram
->
setLogPath
(
$BOT_NAME
.
'.log'
);
```
```
Set verbosity to 3
, to log also
curl requests and responses from the bot to Telegram:
Set verbosity to 3
to also log
curl requests and responses from the bot to Telegram:
```
php
```
php
$telegram
->
setLogRequests
(
true
);
$telegram
->
setLogRequests
(
true
);
$telegram
->
setLogPath
(
$BOT_NAME
.
'.log'
);
$telegram
->
setLogPath
(
$BOT_NAME
.
'.log'
);
$telegram
->
setLogVerbosity
(
3
);
$telegram
->
setLogVerbosity
(
3
);
```
```
-----
## Documentation
This code is available on
[
Github
](
https://github.com/akalongman/php-telegram-bot
)
. Pull requests are welcome.
##Documentation
Take a look at the repo
[
Wiki
](
https://github.com/akalongman/php-telegram-bot/wiki
)
for further information and tutorial!
Take a look at the repo
[
Wiki
](
https://github.com/akalongman/php-telegram-bot/wiki
)
for further information and tutorial!
Feel free to improve!
Feel free to improve!
##Project with this library
## Projects with this library
Here's a list of projects that feats this library, feel free to add yours!
Here's a list of projects that feats this library, feel free to add yours!
-
[
Super-Dice-Roll
](
https://github.com/RafaelDelboni/Super-Dice-Roll
)
[
@superdiceroll_bot
]
(https://telegram.me/superdiceroll_bot)
-
[
Super-Dice-Roll
](
https://github.com/RafaelDelboni/Super-Dice-Roll
)
[
@superdiceroll_bot
]
(https://telegram.me/superdiceroll_bot)
...
@@ -399,14 +461,13 @@ If you like living on the edge, please report any bugs you find on the
...
@@ -399,14 +461,13 @@ If you like living on the edge, please report any bugs you find on the
## Contributing
## Contributing
See
[
CONTRIBUTING
.md
](
CONTRIBUTING.md
)
for
information.
See
[
CONTRIBUTING
](
CONTRIBUTING.md
)
for more
information.
## License
## License
Please see the
[
LICENSE
](
LICENSE.md
)
included in this repository for a full copy of the MIT license,
Please see the
[
LICENSE
](
LICENSE.md
)
included in this repository for a full copy of the MIT license,
which this project is licensed under.
which this project is licensed under.
## Credits
## Credits
Credit list in
[
CREDITS
](
CREDITS
)
Credit list in
[
CREDITS
](
CREDITS
)
examples/getUpdatesCLI.php
View file @
61481e19
...
@@ -7,60 +7,59 @@
...
@@ -7,60 +7,59 @@
#bash script
#bash script
#while true; do ./getUpdatesCLI.php; done
#while true; do ./getUpdatesCLI.php; done
//Composer Loader
// Load composer
$dir
=
realpath
(
__DIR__
.
'/..'
);
require
__DIR__
.
'/vendor/autoload.php'
;
$loader
=
require
$dir
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'namebot'
;
$BOT_NAME
=
'namebot'
;
//$
COMMANDS_FOLDER = __DIR__.
'/Commands/';
//$
commands_path = __DIR__ .
'/Commands/';
$credentials
=
[
$
mysql_
credentials
=
[
'host'
=>
'localhost'
,
'host'
=>
'localhost'
,
'user'
=>
'dbuser'
,
'user'
=>
'dbuser'
,
'password'
=>
'dbpass'
,
'password'
=>
'dbpass'
,
'database'
=>
'dbname'
'database'
=>
'dbname'
,
];
];
try
{
try
{
//
c
reate Telegram API object
//
C
reate Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
//
//Options
//
Enable MySQL
$telegram
->
enableMySQL
(
$credentials
);
$telegram
->
enableMySQL
(
$
mysql_
credentials
);
////
Enable mysql
with table prefix
////
Enable MySQL
with table prefix
//$telegram->enableMySQL($
credentials, $BOT_NAME.
'_');
//$telegram->enableMySQL($
mysql_credentials, $BOT_NAME .
'_');
//$telegram->addCommandsPath($COMMANDS_FOLDER);
//// Add an additional commands path
//$telegram->addCommandsPath($commands_path);
////
Here you can enable admin interface ache
the channel you want to manage
////
Here you can enable admin interface for
the channel you want to manage
//$telegram->enableAdmins(['your_telegram_id']);
//$telegram->enableAdmins(['your_telegram_id']);
//$telegram->setCommandConfig('sendtochannel', ['your_channel'
=>
'@type_here_your_channel']);
//$telegram->setCommandConfig('sendtochannel', ['your_channel'
=>
'@type_here_your_channel']);
////
Here you can set some command specified
parameters,
////
Here you can set some command specific
parameters,
////for example, google geocode/timezone api key for date command:
////
for example, google geocode/timezone api key for date command:
//$telegram->setCommandConfig('date',
array('google_api_key'=>'your_google_api_key_here')
);
//$telegram->setCommandConfig('date',
['google_api_key' => 'your_google_api_key_here']
);
////Logging
////
Logging
//$telegram->setLogRequests(true);
//$telegram->setLogRequests(true);
//$telegram->setLogPath($BOT_NAME
.
'.log');
//$telegram->setLogPath($BOT_NAME
.
'.log');
//$telegram->setLogVerbosity(3);
//$telegram->setLogVerbosity(3);
//$telegram->setDownloadPath("../Download");
//// Set custom Upload and Download path
//$telegram->setUploadPath("../Upload");
//$telegram->setDownloadPath('../Download');
//$telegram->setUploadPath('../Upload');
//
h
andle telegram getUpdate request
//
H
andle telegram getUpdate request
$ServerResponse
=
$telegram
->
handleGetUpdates
();
$ServerResponse
=
$telegram
->
handleGetUpdates
();
if
(
$ServerResponse
->
isOk
())
{
if
(
$ServerResponse
->
isOk
())
{
$n_update
=
count
(
$ServerResponse
->
getResult
());
$n_update
=
count
(
$ServerResponse
->
getResult
());
print
(
date
(
'Y-m-d H:i:s'
,
time
())
.
' - Processed '
.
$n_update
.
' updates'
.
"
\n
"
);
print
(
date
(
'Y-m-d H:i:s'
,
time
())
.
' - Processed '
.
$n_update
.
" updates
\n
"
);
}
else
{
}
else
{
print
(
date
(
'Y-m-d H:i:s'
,
time
())
.
" - Fail fetch updates
\n
"
);
print
(
date
(
'Y-m-d H:i:s'
,
time
())
.
' - Failed to fetch updates'
.
"
\n
"
);
echo
$ServerResponse
->
printError
()
.
"
\n
"
;
echo
$ServerResponse
->
printError
()
.
"
\n
"
;
}
}
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
// log telegram errors
// log telegram errors
echo
$e
;
echo
$e
;
...
...
examples/hook.php
View file @
61481e19
<?php
<?php
//README
//README
//This configuration file is inten
t
ed to run the bot with the webhook method.
//This configuration file is inten
d
ed to run the bot with the webhook method.
//Uncommented parameters must be filled
//Uncommented parameters must be filled
//Please notice that if you open this file with your browser you'll get the "Input is empty!" Exception.
//Please notice that if you open this file with your browser you'll get the "Input is empty!" Exception.
//This is a normal behaviour because this address has to be reached only by Telegram server
//This is a normal behaviour because this address has to be reached only by Telegram server
//Composer Loader
// Load composer
$dir
=
realpath
(
__DIR__
.
'/..'
);
require
__DIR__
.
'/vendor/autoload.php'
;
$loader
=
require
$dir
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'namebot'
;
$BOT_NAME
=
'namebot'
;
//$
COMMANDS_FOLDER = __DIR__.
'/Commands/';
//$
commands_path = __DIR__ .
'/Commands/';
//$credentials = [
//$
mysql_
credentials = [
// 'host'
=>
'localhost',
// 'host'
=>
'localhost',
// 'user'
=>
'dbuser',
// 'user'
=>
'dbuser',
// 'password'
=>
'dbpass',
// 'password'
=>
'dbpass',
// 'database'
=>'dbname'
// 'database'
=> 'dbname',
//];
//];
try
{
try
{
//
c
reate Telegram API object
//
C
reate Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
////
Options
////
Enable MySQL
$telegram
->
enableMySQL
(
$
credentials
);
//$telegram->enableMySQL($mysql_
credentials);
////
Enable mysql
with table prefix
////
Enable MySQL
with table prefix
//$telegram->enableMySQL($
credentials, $BOT_NAME.
'_');
//$telegram->enableMySQL($
mysql_credentials, $BOT_NAME .
'_');
//$telegram->addCommandsPath($COMMANDS_FOLDER);
//// Add an additional commands path
//$telegram->addCommandsPath($commands_path);
////
Here you can enable admin interface ache
the channel you want to manage
////
Here you can enable admin interface for
the channel you want to manage
//$telegram->enableAdmins(['your_telegram_id']);
//$telegram->enableAdmins(['your_telegram_id']);
//$telegram->setCommandConfig('sendtochannel', ['your_channel'
=>
'@type_here_your_channel']);
//$telegram->setCommandConfig('sendtochannel', ['your_channel'
=>
'@type_here_your_channel']);
////
Here you can set some command specified
parameters,
////
Here you can set some command specific
parameters,
////for example, google geocode/timezone api key for date command:
////
for example, google geocode/timezone api key for date command:
//$telegram->setCommandConfig('date',
array('google_api_key'=>'your_google_api_key_here')
);
//$telegram->setCommandConfig('date',
['google_api_key' => 'your_google_api_key_here']
);
////Logging
////
Logging
//$telegram->setLogRequests(true);
//$telegram->setLogRequests(true);
//$telegram->setLogPath($BOT_NAME
.
'.log');
//$telegram->setLogPath($BOT_NAME
.
'.log');
//$telegram->setLogVerbosity(3);
//$telegram->setLogVerbosity(3);
//$telegram->setDownloadPath("../Download");
//// Set custom Upload and Download path
//$telegram->setUploadPath("../Upload");
//$telegram->setDownloadPath('../Download');
//$telegram->setUploadPath('../Upload');
//
h
andle telegram webhook request
//
H
andle telegram webhook request
$telegram
->
handle
();
$telegram
->
handle
();
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
//
Silence is gold
!
//
Silence is golden
!
// log telegram errors
// log telegram errors
// echo $e;
// echo $e;
}
}
examples/set.php
View file @
61481e19
<?php
<?php
//Composer Loader
// Load composer
$dir
=
realpath
(
__DIR__
.
'/..'
);
require
__DIR__
.
'/vendor/autoload.php'
;
$loader
=
require
$dir
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'namebot'
;
$BOT_NAME
=
'namebot'
;
$
link
=
'https://yourdomain/yourpath_to_
hook.php'
;
$
hook_url
=
'https://yourdomain/path/to/
hook.php'
;
try
{
try
{
//
c
reate Telegram API object
//
C
reate Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
// set webhook
$result
=
$telegram
->
setWebHook
(
$link
);
// Set webhook
//Uncomment to use certificate
$result
=
$telegram
->
setWebHook
(
$hook_url
);
//$result = $telegram->setWebHook($link, $path_certificate);
// Uncomment to use certificate
//$result = $telegram->setWebHook($hook_url, $path_certificate);
if
(
$result
->
isOk
())
{
if
(
$result
->
isOk
())
{
echo
$result
->
getDescription
();
echo
$result
->
getDescription
();
}
}
...
...
examples/unset.php
View file @
61481e19
<?php
<?php
//
Composer Load
er
//
Load compos
er
$loader
=
require
__DIR__
.
'/vendor/autoload.php'
;
require
__DIR__
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'namebot'
;
$BOT_NAME
=
'namebot'
;
try
{
try
{
//
c
reate Telegram API object
//
C
reate Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
// set webhook
// Unset webhook
$result
=
$telegram
->
unsetWebHook
();
$result
=
$telegram
->
unsetWebHook
();
//Uncomment to use certificate
//$result = $telegram->setWebHook($link, $path_certificate);
if
(
$result
->
isOk
())
{
if
(
$result
->
isOk
())
{
echo
$result
->
getDescription
();
echo
$result
->
getDescription
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment