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
3228a654
Commit
3228a654
authored
Apr 08, 2017
by
Armando Lüscher
Committed by
GitHub
Apr 08, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #457 from jacklul/misc
Misc additions/changes
parents
036bac23
b3a7d3e2
Changes
15
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
127 additions
and
61 deletions
+127
-61
getUpdatesCLI.php
examples/getUpdatesCLI.php
+2
-2
hook.php
examples/hook.php
+2
-2
set.php
examples/set.php
+3
-2
unset.php
examples/unset.php
+3
-2
Entity.php
src/Entities/Entity.php
+21
-7
Message.php
src/Entities/Message.php
+3
-3
ReplyToMessage.php
src/Entities/ReplyToMessage.php
+3
-3
ServerResponse.php
src/Entities/ServerResponse.php
+11
-11
Request.php
src/Request.php
+3
-3
Telegram.php
src/Telegram.php
+50
-16
CommandTest.php
tests/unit/Commands/CommandTest.php
+2
-2
CommandTestCase.php
tests/unit/Commands/CommandTestCase.php
+1
-1
ConversationTest.php
tests/unit/ConversationTest.php
+1
-1
TelegramTest.php
tests/unit/TelegramTest.php
+17
-6
TestCase.php
tests/unit/TestCase.php
+5
-0
No files found.
examples/getUpdatesCLI.php
View file @
3228a654
...
...
@@ -14,7 +14,7 @@ require __DIR__ . '/vendor/autoload.php';
// Add you bot's API key and name
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'username_bot'
;
$BOT_
USER
NAME
=
'username_bot'
;
// Define a path for your custom commands
//$commands_path = __DIR__ . '/Commands/';
...
...
@@ -29,7 +29,7 @@ $mysql_credentials = [
try
{
// Create Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_
USER
NAME
);
// Error, Debug and Raw Update logging
//Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);
...
...
examples/hook.php
View file @
3228a654
...
...
@@ -13,7 +13,7 @@ require __DIR__ . '/vendor/autoload.php';
// Add you bot's API key and name
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'username_bot'
;
$BOT_
USER
NAME
=
'username_bot'
;
// Define a path for your custom commands
//$commands_path = __DIR__ . '/Commands/';
...
...
@@ -28,7 +28,7 @@ $BOT_NAME = 'username_bot';
try
{
// Create Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_
USER
NAME
);
// Error, Debug and Raw Update logging
//Longman\TelegramBot\TelegramLog::initialize($your_external_monolog_instance);
...
...
examples/set.php
View file @
3228a654
...
...
@@ -3,11 +3,12 @@
require
__DIR__
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'username_bot'
;
$BOT_
USER
NAME
=
'username_bot'
;
$hook_url
=
'https://yourdomain/path/to/hook.php'
;
try
{
// Create Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_
USER
NAME
);
// Set webhook
$result
=
$telegram
->
setWebhook
(
$hook_url
);
...
...
examples/unset.php
View file @
3228a654
...
...
@@ -3,10 +3,11 @@
require
__DIR__
.
'/vendor/autoload.php'
;
$API_KEY
=
'your_bot_api_key'
;
$BOT_NAME
=
'username_bot'
;
$BOT_USERNAME
=
'username_bot'
;
try
{
// Create Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_
USER
NAME
);
// Delete webhook
$result
=
$telegram
->
deleteWebhook
();
...
...
src/Entities/Entity.php
View file @
3228a654
...
...
@@ -12,6 +12,7 @@ namespace Longman\TelegramBot\Entities;
use
Exception
;
use
Longman\TelegramBot\Entities\InlineQuery\InlineEntity
;
use
Longman\TelegramBot\TelegramLog
;
use
ReflectionObject
;
/**
...
...
@@ -21,22 +22,22 @@ use ReflectionObject;
*
* @link https://core.telegram.org/bots/api#available-types
*
* @method array getRawData() Get the raw data passed to this entity
* @method string getBot
N
ame() Return the bot name passed to this entity
* @method array getRawData()
Get the raw data passed to this entity
* @method string getBot
Usern
ame() Return the bot name passed to this entity
*/
abstract
class
Entity
{
/**
* Entity constructor.
*
* @todo Get rid of the $bot_name, it shouldn't be here!
* @todo Get rid of the $bot_
user
name, it shouldn't be here!
*
* @param array $data
* @param string $bot_name
* @param string $bot_
user
name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
$data
,
$bot_name
=
''
)
public
function
__construct
(
$data
,
$bot_
user
name
=
''
)
{
//Make sure we're not raw_data inception-ing
if
(
array_key_exists
(
'raw_data'
,
$data
))
{
...
...
@@ -47,7 +48,7 @@ abstract class Entity
$data
[
'raw_data'
]
=
$data
;
}
$data
[
'bot_
name'
]
=
$bot_
name
;
$data
[
'bot_
username'
]
=
$bot_user
name
;
$this
->
assignMemberVariables
(
$data
);
$this
->
validate
();
}
...
...
@@ -142,7 +143,7 @@ abstract class Entity
$sub_entities
=
$this
->
subEntities
();
if
(
isset
(
$sub_entities
[
$property_name
]))
{
return
new
$sub_entities
[
$property_name
](
$property
,
$this
->
getProperty
(
'bot_name'
));
return
new
$sub_entities
[
$property_name
](
$property
,
$this
->
getProperty
(
'bot_
user
name'
));
}
return
$property
;
...
...
@@ -241,4 +242,17 @@ abstract class Entity
return
(
$is_username
?
'@'
:
''
)
.
$name
;
}
/**
* Get Bot name
*
* @todo: Left for backwards compatibility, remove in the future
*
* @return string
*/
public
function
getBotName
()
{
TelegramLog
::
debug
(
'Usage of deprecated method getBotName() detected, please use getBotUsername() instead!'
);
return
$this
->
getBotUsername
();
}
}
src/Entities/Message.php
View file @
3228a654
...
...
@@ -79,11 +79,11 @@ class Message extends Entity
* Message constructor
*
* @param array $data
* @param string $bot_name
* @param string $bot_
user
name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
array
$data
,
$bot_name
=
''
)
public
function
__construct
(
array
$data
,
$bot_
user
name
=
''
)
{
//Retro-compatibility
if
(
isset
(
$data
[
'new_chat_participant'
]))
{
...
...
@@ -95,7 +95,7 @@ class Message extends Entity
unset
(
$data
[
'left_chat_participant'
]);
}
parent
::
__construct
(
$data
,
$bot_name
);
parent
::
__construct
(
$data
,
$bot_
user
name
);
}
/**
...
...
src/Entities/ReplyToMessage.php
View file @
3228a654
...
...
@@ -21,16 +21,16 @@ class ReplyToMessage extends Message
* ReplyToMessage constructor.
*
* @param array $data
* @param string $bot_name
* @param string $bot_
user
name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
array
$data
,
$bot_name
=
''
)
public
function
__construct
(
array
$data
,
$bot_
user
name
=
''
)
{
//As explained in the documentation
//Reply to message can't contain other reply to message entities
unset
(
$data
[
'reply_to_message'
]);
parent
::
__construct
(
$data
,
$bot_name
);
parent
::
__construct
(
$data
,
$bot_
user
name
);
}
}
src/Entities/ServerResponse.php
View file @
3228a654
...
...
@@ -26,11 +26,11 @@ class ServerResponse extends Entity
* ServerResponse constructor.
*
* @param array $data
* @param string $bot_name
* @param string $bot_
user
name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
array
$data
,
$bot_name
)
public
function
__construct
(
array
$data
,
$bot_
user
name
)
{
// Make sure we don't double-save the raw_data
unset
(
$data
[
'raw_data'
]);
...
...
@@ -41,13 +41,13 @@ class ServerResponse extends Entity
if
(
$is_ok
&&
is_array
(
$result
))
{
if
(
$this
->
isAssoc
(
$result
))
{
$data
[
'result'
]
=
$this
->
createResultObject
(
$result
,
$bot_name
);
$data
[
'result'
]
=
$this
->
createResultObject
(
$result
,
$bot_
user
name
);
}
else
{
$data
[
'result'
]
=
$this
->
createResultObjects
(
$result
,
$bot_name
);
$data
[
'result'
]
=
$this
->
createResultObjects
(
$result
,
$bot_
user
name
);
}
}
parent
::
__construct
(
$data
,
$bot_name
);
parent
::
__construct
(
$data
,
$bot_
user
name
);
}
/**
...
...
@@ -88,12 +88,12 @@ class ServerResponse extends Entity
* Create and return the object of the received result
*
* @param array $result
* @param string $bot_name
* @param string $bot_
user
name
*
* @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private
function
createResultObject
(
$result
,
$bot_name
)
private
function
createResultObject
(
$result
,
$bot_
user
name
)
{
// We don't need to save the raw_data of the response object!
$result
[
'raw_data'
]
=
null
;
...
...
@@ -115,19 +115,19 @@ class ServerResponse extends Entity
}
//Response from sendMessage
return
new
Message
(
$result
,
$bot_name
);
return
new
Message
(
$result
,
$bot_
user
name
);
}
/**
* Create and return the objects array of the received result
*
* @param array $result
* @param string $bot_name
* @param string $bot_
user
name
*
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
private
function
createResultObjects
(
$result
,
$bot_name
)
private
function
createResultObjects
(
$result
,
$bot_
user
name
)
{
$results
=
[];
if
(
isset
(
$result
[
0
][
'user'
]))
{
...
...
@@ -144,7 +144,7 @@ class ServerResponse extends Entity
// We don't need to save the raw_data of the response object!
$update
[
'raw_data'
]
=
null
;
$results
[]
=
new
Update
(
$update
,
$bot_name
);
$results
[]
=
new
Update
(
$update
,
$bot_
user
name
);
}
}
...
...
src/Request.php
View file @
3228a654
...
...
@@ -315,12 +315,12 @@ class Request
{
self
::
ensureValidAction
(
$action
);
$bot_
name
=
self
::
$telegram
->
getBotN
ame
();
$bot_
username
=
self
::
$telegram
->
getBotUsern
ame
();
if
(
defined
(
'PHPUNIT_TESTSUITE'
))
{
$fake_response
=
self
::
generateGeneralFakeServerResponse
(
$data
);
return
new
ServerResponse
(
$fake_response
,
$bot_name
);
return
new
ServerResponse
(
$fake_response
,
$bot_
user
name
);
}
self
::
ensureNonEmptyData
(
$data
);
...
...
@@ -333,7 +333,7 @@ class Request
throw
new
TelegramException
(
'Telegram returned an invalid response! Please review your bot name and API key.'
);
}
return
new
ServerResponse
(
$response
,
$bot_name
);
return
new
ServerResponse
(
$response
,
$bot_
user
name
);
}
/**
...
...
src/Telegram.php
View file @
3228a654
...
...
@@ -40,11 +40,18 @@ class Telegram
protected
$api_key
=
''
;
/**
* Telegram Bot name
* Telegram Bot
user
name
*
* @var string
*/
protected
$bot_name
=
''
;
protected
$bot_username
=
''
;
/**
* Telegram Bot id
*
* @var string
*/
protected
$bot_id
=
''
;
/**
* Raw request data (json) for webhook methods
...
...
@@ -127,22 +134,26 @@ class Telegram
* Telegram constructor.
*
* @param string $api_key
* @param string $bot_name
* @param string $bot_
user
name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
$api_key
,
$bot_name
)
public
function
__construct
(
$api_key
,
$bot_
user
name
)
{
if
(
empty
(
$api_key
))
{
throw
new
TelegramException
(
'API KEY not defined!'
);
}
preg_match
(
'/(\d+)\:[\w\-]+/'
,
$api_key
,
$matches
);
if
(
!
isset
(
$matches
[
1
]))
{
throw
new
TelegramException
(
'Invalid API KEY defined!'
);
}
$this
->
bot_id
=
$matches
[
1
];
$this
->
api_key
=
$api_key
;
if
(
empty
(
$bot_name
))
{
if
(
empty
(
$bot_
user
name
))
{
throw
new
TelegramException
(
'Bot Username not defined!'
);
}
$this
->
api_key
=
$api_key
;
$this
->
bot_name
=
$bot_name
;
$this
->
bot_username
=
$bot_username
;
//Set default download and upload path
$this
->
setDownloadPath
(
BASE_PATH
.
'/../Download'
);
...
...
@@ -309,7 +320,7 @@ class Telegram
'ok'
=>
false
,
'description'
=>
'getUpdates needs MySQL connection!'
,
],
$this
->
bot_name
$this
->
bot_
user
name
);
}
...
...
@@ -359,7 +370,7 @@ class Telegram
throw
new
TelegramException
(
'Invalid JSON!'
);
}
if
(
$response
=
$this
->
processUpdate
(
new
Update
(
$post
,
$this
->
bot_name
)))
{
if
(
$response
=
$this
->
processUpdate
(
new
Update
(
$post
,
$this
->
bot_
user
name
)))
{
return
$response
->
isOk
();
}
...
...
@@ -723,9 +734,32 @@ class Telegram
*
* @return string
*/
public
function
getBotUsername
()
{
return
$this
->
bot_username
;
}
/**
* Get Bot name
*
* @todo: Left for backwards compatibility, remove in the future
*
* @return string
*/
public
function
getBotName
()
{
return
$this
->
bot_name
;
TelegramLog
::
debug
(
'Usage of deprecated method getBotName() detected, please use getBotUsername() instead!'
);
return
$this
->
getBotUsername
();
}
/**
* Get Bot Id
*
* @return string
*/
public
function
getBotId
()
{
return
$this
->
bot_id
;
}
/**
...
...
@@ -828,7 +862,7 @@ class Telegram
* Enable Botan.io integration
*
* @param string $token
* @param array $options
* @param array
$options
*
* @return \Longman\TelegramBot\Telegram
* @throws \Longman\TelegramBot\Exception\TelegramException
...
...
@@ -850,7 +884,7 @@ class Telegram
return
$this
;
}
/**
* Run provided commands
*
...
...
@@ -888,15 +922,15 @@ class Telegram
'from'
=>
[
'id'
=>
$bot_id
,
'first_name'
=>
$bot_name
,
'username'
=>
$bot_username
'username'
=>
$bot_username
,
],
'date'
=>
time
(),
'chat'
=>
[
'id'
=>
$bot_id
,
'type'
=>
'private'
,
],
'text'
=>
$command
]
'text'
=>
$command
,
]
,
]
);
...
...
tests/unit/Commands/CommandTest.php
View file @
3228a654
...
...
@@ -51,11 +51,11 @@ class CommandTest extends TestCase
public
function
setUp
()
{
//Default command object
$this
->
telegram
=
new
Telegram
(
'apikey'
,
'testbot'
);
$this
->
telegram
=
new
Telegram
(
self
::
$dummy_api_key
,
'testbot'
);
$this
->
command_stub
=
$this
->
getMockForAbstractClass
(
$this
->
command_namespace
,
[
$this
->
telegram
]);
//Create separate command object that contain a command config
$this
->
telegram_with_config
=
new
Telegram
(
'apikey'
,
'testbot'
);
$this
->
telegram_with_config
=
new
Telegram
(
self
::
$dummy_api_key
,
'testbot'
);
$this
->
telegram_with_config
->
setCommandConfig
(
'command_name'
,
[
'config_key'
=>
'config_value'
]);
$this
->
command_stub_with_config
=
$this
->
getMockBuilder
(
$this
->
command_namespace
)
->
disableOriginalConstructor
()
...
...
tests/unit/Commands/CommandTestCase.php
View file @
3228a654
...
...
@@ -37,7 +37,7 @@ class CommandTestCase extends TestCase
*/
public
function
setUp
()
{
$this
->
telegram
=
new
Telegram
(
'apikey'
,
'testbot'
);
$this
->
telegram
=
new
Telegram
(
self
::
$dummy_api_key
,
'testbot'
);
$this
->
telegram
->
addCommandsPath
(
BASE_COMMANDS_PATH
.
'/UserCommands'
);
// Add custom commands dedicated to do some tests.
...
...
tests/unit/ConversationTest.php
View file @
3228a654
...
...
@@ -39,7 +39,7 @@ class ConversationTest extends TestCase
'password'
=>
PHPUNIT_DB_PASS
,
];
$this
->
telegram
=
new
Telegram
(
'apikey'
,
'testbot'
);
$this
->
telegram
=
new
Telegram
(
self
::
$dummy_api_key
,
'testbot'
);
$this
->
telegram
->
enableMySql
(
$credentials
);
//Make sure we start with an empty DB for each test.
...
...
tests/unit/TelegramTest.php
View file @
3228a654
...
...
@@ -40,7 +40,7 @@ class TelegramTest extends TestCase
*/
protected
function
setUp
()
{
$this
->
telegram
=
new
Telegram
(
'apikey'
,
'testbot'
);
$this
->
telegram
=
new
Telegram
(
self
::
$dummy_api_key
,
'testbot'
);
// Create a few dummy custom commands paths.
foreach
(
$this
->
custom_commands_paths
as
$custom_path
)
{
...
...
@@ -61,6 +61,7 @@ class TelegramTest extends TestCase
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage API KEY not defined!
*/
public
function
testNewInstanceWithoutApiKeyParam
()
{
...
...
@@ -69,20 +70,30 @@ class TelegramTest extends TestCase
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Invalid API KEY defined!
*/
public
function
testNewInstanceWith
outBotName
Param
()
public
function
testNewInstanceWith
InvalidApiKey
Param
()
{
new
Telegram
(
'apikey'
,
null
);
new
Telegram
(
'invalid-api-key-format'
,
null
);
}
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Bot Username not defined!
*/
public
function
testNewInstanceWithoutBotUsernameParam
()
{
new
Telegram
(
self
::
$dummy_api_key
,
null
);
}
public
function
testGetApiKey
()
{
$this
->
assertEquals
(
'apikey'
,
$this
->
telegram
->
getApiKey
());
$this
->
assertEquals
(
self
::
$dummy_api_key
,
$this
->
telegram
->
getApiKey
());
}
public
function
testGetBot
N
ame
()
public
function
testGetBot
Usern
ame
()
{
$this
->
assertEquals
(
'testbot'
,
$this
->
telegram
->
getBot
N
ame
());
$this
->
assertEquals
(
'testbot'
,
$this
->
telegram
->
getBot
Usern
ame
());
}
public
function
testEnableAdmins
()
...
...
tests/unit/TestCase.php
View file @
3228a654
...
...
@@ -12,6 +12,11 @@ namespace Longman\TelegramBot\Tests\Unit;
class
TestCase
extends
\PHPUnit_Framework_TestCase
{
/**
* @var string
*/
public
static
$dummy_api_key
=
'123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'
;
protected
function
skip64BitTest
()
{
if
(
PHP_INT_SIZE
===
4
)
{
...
...
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