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
5caebd47
Commit
5caebd47
authored
Oct 17, 2015
by
MBoretto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
All methods implemented
parent
9179a987
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
280 additions
and
31 deletions
+280
-31
WhoamiCommand.php
src/Commands/WhoamiCommand.php
+52
-6
Chat.php
src/Entities/Chat.php
+10
-1
ServerResponse.php
src/Entities/ServerResponse.php
+17
-12
UserProfilePhotos.php
src/Entities/UserProfilePhotos.php
+4
-3
Request.php
src/Request.php
+49
-8
Telegram.php
src/Telegram.php
+70
-1
ServerResponseTest.php
tests/Unit/Entities/ServerResponseTest.php
+78
-0
No files found.
src/Commands/WhoamiCommand.php
View file @
5caebd47
...
@@ -31,19 +31,65 @@ class WhoamiCommand extends Command
...
@@ -31,19 +31,65 @@ class WhoamiCommand extends Command
$update
=
$this
->
getUpdate
();
$update
=
$this
->
getUpdate
();
$message
=
$this
->
getMessage
();
$message
=
$this
->
getMessage
();
$user_id
=
$message
->
getFrom
()
->
getId
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$chat_id
=
$message
->
getChat
()
->
getId
();
$message_id
=
$message
->
getMessageId
();
$message_id
=
$message
->
getMessageId
();
$text
=
$message
->
getText
(
true
);
$text
=
$message
->
getText
(
true
);
$data
=
array
();
//send chat action
Request
::
sendChatAction
([
'chat_id'
=>
$chat_id
,
'action'
=>
'typing'
]);
$caption
=
'Your Id: '
.
$message
->
getFrom
()
->
getId
();
$caption
.=
"
\n
"
.
'Name: '
.
$message
->
getFrom
()
->
getFirstName
()
.
' '
.
$message
->
getFrom
()
->
getLastName
();
$caption
.=
"
\n
"
.
'Username: '
.
$message
->
getFrom
()
->
getUsername
();
$limit
=
10
;
$offset
=
null
;
$ServerResponse
=
Request
::
getUserProfilePhotos
([
'user_id'
=>
$user_id
,
'limit'
=>
$limit
,
'offset'
=>
$offset
]);
//Check if the request isOK
if
(
$ServerResponse
->
isOk
()){
$UserProfilePhoto
=
$ServerResponse
->
getResult
();
$totalcount
=
$UserProfilePhoto
->
getTotalCount
();
}
else
{
$totalcount
=
0
;
}
//$photos = $UserProfilePhoto->getPhotos();
////I pick the latest photo with the hight definition
//$photo = $photos[0][2];
//$file_id = $photo->getFileId();
//$ServerResponse = Request::getFile(['file_id' => $file_id]);
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
$data
[
'chat_id'
]
=
$chat_id
;
$data
[
'reply_to_message_id'
]
=
$message_id
;
$data
[
'reply_to_message_id'
]
=
$message_id
;
$data
[
'text'
]
=
'Your Id: '
.
$message
->
getFrom
()
->
getId
();
$data
[
'text'
]
.=
"
\n
"
.
'Name: '
.
$message
->
getFrom
()
->
getFirstName
()
.
' '
.
$message
->
getFrom
()
->
getLastName
();
$data
[
'text'
]
.=
"
\n
"
.
'Username: '
.
$message
->
getFrom
()
->
getUsername
();
$result
=
Request
::
sendMessage
(
$data
);
if
(
$totalcount
>
0
){
$photos
=
$UserProfilePhoto
->
getPhotos
();
//I pick the latest photo with the hight definition
$photo
=
$photos
[
0
][
2
];
$file_id
=
$photo
->
getFileId
();
$data
[
'photo'
]
=
$file_id
;
$data
[
'caption'
]
=
$caption
;
$result
=
Request
::
sendPhoto
(
$data
);
}
else
{
$data
[
'text'
]
=
$caption
;
$result
=
Request
::
sendMessage
(
$data
);
}
return
$result
;
return
$result
;
}
}
}
}
src/Entities/Chat.php
View file @
5caebd47
...
@@ -16,10 +16,11 @@ class Chat extends Entity
...
@@ -16,10 +16,11 @@ class Chat extends Entity
{
{
protected
$id
;
protected
$id
;
protected
$type
;
protected
$title
;
protected
$title
;
protected
$username
;
protected
$first_name
;
protected
$first_name
;
protected
$last_name
;
protected
$last_name
;
protected
$username
;
public
function
__construct
(
array
$data
)
public
function
__construct
(
array
$data
)
{
{
...
@@ -29,6 +30,8 @@ class Chat extends Entity
...
@@ -29,6 +30,8 @@ class Chat extends Entity
throw
new
TelegramException
(
'id is empty!'
);
throw
new
TelegramException
(
'id is empty!'
);
}
}
$this
->
type
=
isset
(
$data
[
'type'
])
?
$data
[
'type'
]
:
null
;
$this
->
title
=
isset
(
$data
[
'title'
])
?
$data
[
'title'
]
:
null
;
$this
->
title
=
isset
(
$data
[
'title'
])
?
$data
[
'title'
]
:
null
;
$this
->
first_name
=
isset
(
$data
[
'first_name'
])
?
$data
[
'first_name'
]
:
null
;
$this
->
first_name
=
isset
(
$data
[
'first_name'
])
?
$data
[
'first_name'
]
:
null
;
$this
->
last_name
=
isset
(
$data
[
'last_name'
])
?
$data
[
'last_name'
]
:
null
;
$this
->
last_name
=
isset
(
$data
[
'last_name'
])
?
$data
[
'last_name'
]
:
null
;
...
@@ -57,6 +60,12 @@ class Chat extends Entity
...
@@ -57,6 +60,12 @@ class Chat extends Entity
return
$this
->
id
;
return
$this
->
id
;
}
}
public
function
getType
()
{
return
$this
->
type
;
}
public
function
getTitle
()
public
function
getTitle
()
{
{
...
...
src/Entities/ServerResponse.php
View file @
5caebd47
...
@@ -28,24 +28,26 @@ class ServerResponse extends Entity
...
@@ -28,24 +28,26 @@ class ServerResponse extends Entity
if
(
isset
(
$data
[
'ok'
])
&
isset
(
$data
[
'result'
]))
{
if
(
isset
(
$data
[
'ok'
])
&
isset
(
$data
[
'result'
]))
{
if
(
is_array
(
$data
[
'result'
]))
{
if
(
is_array
(
$data
[
'result'
]))
{
if
(
$data
[
'ok'
]
&
!
$this
->
isAssoc
(
$data
[
'result'
]))
{
if
(
$data
[
'ok'
]
&
!
$this
->
isAssoc
(
$data
[
'result'
]))
{
//update id
//get update
$this
->
ok
=
$data
[
'ok'
];
//$this->result =[];
foreach
(
$data
[
'result'
]
as
$update
)
{
foreach
(
$data
[
'result'
]
as
$update
)
{
$this
->
result
[]
=
new
Update
(
$update
,
$bot_name
);
$this
->
result
[]
=
new
Update
(
$update
,
$bot_name
);
}
}
$this
->
error_code
=
null
;
$this
->
description
=
null
;
}
elseif
(
$data
[
'ok'
]
&
$this
->
isAssoc
(
$data
[
'result'
]))
{
}
elseif
(
$data
[
'ok'
]
&
$this
->
isAssoc
(
$data
[
'result'
]))
{
//Response from sendMessage set
if
(
isset
(
$data
[
'result'
][
'total_count'
]))
{
$this
->
ok
=
$data
[
'ok'
];
//getUserProfilePhotos
$this
->
result
=
new
Message
(
$data
[
'result'
],
$bot_name
);
$this
->
result
=
new
UserProfilePhotos
(
$data
[
'result'
]);
$this
->
error_code
=
null
;
}
elseif
(
isset
(
$data
[
'result'
][
'file_id'
]))
{
$this
->
description
=
null
;
//Response getFile
$this
->
result
=
new
File
(
$data
[
'result'
]);
}
else
{
//Response from sendMessage
$this
->
result
=
new
Message
(
$data
[
'result'
],
$bot_name
);
}
}
}
$this
->
ok
=
$data
[
'ok'
];
$this
->
error_code
=
null
;
$this
->
description
=
null
;
}
else
{
}
else
{
if
(
$data
[
'ok'
]
&
$data
[
'result'
]
==
true
)
{
if
(
$data
[
'ok'
]
&
$data
[
'result'
]
==
true
)
{
...
@@ -115,4 +117,7 @@ class ServerResponse extends Entity
...
@@ -115,4 +117,7 @@ class ServerResponse extends Entity
{
{
return
$this
->
description
;
return
$this
->
description
;
}
}
public
function
printError
(){
return
'Error N: '
.
$this
->
getErrorCode
()
.
' Description: '
.
$this
->
getDescription
();
}
}
}
src/Entities/UserProfilePhotos.php
View file @
5caebd47
...
@@ -30,11 +30,12 @@ class UserProfilePhotos extends Entity
...
@@ -30,11 +30,12 @@ class UserProfilePhotos extends Entity
throw
new
TelegramException
(
'photos is empty!'
);
throw
new
TelegramException
(
'photos is empty!'
);
}
}
//TODO check
$photos
=
[];
$photos
=
[];
foreach
(
$this
->
photos
as
$key
=>
$photo
s
)
{
foreach
(
$this
->
photos
as
$key
=>
$photo
)
{
if
(
is_array
(
$photo
))
{
if
(
is_array
(
$photo
))
{
$photos
[]
=
[
new
PhotoSize
(
$photo
[
0
])];
foreach
(
$photo
as
$photo_size
)
{
$photos
[
$key
][]
=
new
PhotoSize
(
$photo_size
);
}
}
else
{
}
else
{
throw
new
TelegramException
(
'photo is not an array!'
);
throw
new
TelegramException
(
'photo is not an array!'
);
}
}
...
...
src/Request.php
View file @
5caebd47
...
@@ -33,6 +33,7 @@ class Request
...
@@ -33,6 +33,7 @@ class Request
'getUserProfilePhotos'
,
'getUserProfilePhotos'
,
'getUpdates'
,
'getUpdates'
,
'setWebhook'
,
'setWebhook'
,
'getFile'
);
);
public
static
function
initialize
(
Telegram
$telegram
)
public
static
function
initialize
(
Telegram
$telegram
)
...
@@ -166,6 +167,11 @@ class Request
...
@@ -166,6 +167,11 @@ class Request
return
$result
;
return
$result
;
}
}
protected
static
function
encodeFile
(
$file
)
{
return
new
\CURLFile
(
$file
);
}
public
static
function
send
(
$action
,
array
$data
=
null
)
public
static
function
send
(
$action
,
array
$data
=
null
)
{
{
if
(
!
in_array
(
$action
,
self
::
$methods
))
{
if
(
!
in_array
(
$action
,
self
::
$methods
))
{
...
@@ -179,6 +185,9 @@ class Request
...
@@ -179,6 +185,9 @@ class Request
$result
=
self
::
executeCurl
(
$action
,
$data
);
$result
=
self
::
executeCurl
(
$action
,
$data
);
echo
$result
;
print_r
(
json_decode
(
$result
,
true
));
$bot_name
=
self
::
$telegram
->
getBotName
();
$bot_name
=
self
::
$telegram
->
getBotName
();
return
new
ServerResponse
(
json_decode
(
$result
,
true
),
$bot_name
);
return
new
ServerResponse
(
json_decode
(
$result
,
true
),
$bot_name
);
}
}
...
@@ -200,11 +209,15 @@ class Request
...
@@ -200,11 +209,15 @@ class Request
return
$result
;
return
$result
;
}
}
//TODO forwardMessage
public
static
function
forwardMessage
(
array
$data
)
protected
static
function
encodeFile
(
$file
)
{
{
return
new
\CURLFile
(
$file
);
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
$result
=
self
::
send
(
'forwardMessage'
,
$data
);
return
$result
;
}
}
public
static
function
sendPhoto
(
array
$data
,
$file
=
null
)
public
static
function
sendPhoto
(
array
$data
,
$file
=
null
)
...
@@ -310,7 +323,18 @@ class Request
...
@@ -310,7 +323,18 @@ class Request
return
$result
;
return
$result
;
}
}
//getUserProfilePhotos
public
static
function
getUserProfilePhotos
(
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
if
(
!
isset
(
$data
[
'user_id'
]))
{
throw
new
TelegramException
(
'User id is empty!'
);
}
$result
=
self
::
send
(
'getUserProfilePhotos'
,
$data
);
return
$result
;
}
public
static
function
getUpdates
(
$data
)
public
static
function
getUpdates
(
$data
)
{
{
...
@@ -318,14 +342,31 @@ class Request
...
@@ -318,14 +342,31 @@ class Request
return
$result
;
return
$result
;
}
}
public
static
function
setWebhook
(
$url
)
public
static
function
setWebhook
(
$url
,
$file
=
null
)
{
{
$result
=
self
::
send
(
'setWebhook'
,
array
(
'url'
=>
$url
));
if
(
empty
(
$url
))
{
throw
new
TelegramException
(
'Url is empty!'
);
}
$data
[
'url'
]
=
$url
;
if
(
!
is_null
(
$file
))
{
$data
[
'certificate'
]
=
self
::
encodeFile
(
$file
);
}
$result
=
self
::
send
(
'setWebhook'
,
$data
);
return
$result
;
return
$result
;
}
}
//getFile
public
static
function
getFile
(
$data
)
{
if
(
empty
(
$data
))
{
throw
new
TelegramException
(
'Data is empty!'
);
}
$result
=
self
::
send
(
'getFile'
,
$data
);
return
$result
;
}
/**
/**
* Send Message in all the active chat
* Send Message in all the active chat
*
*
...
...
src/Telegram.php
View file @
5caebd47
...
@@ -30,7 +30,7 @@ class Telegram
...
@@ -30,7 +30,7 @@ class Telegram
*
*
* @var string
* @var string
*/
*/
protected
$version
=
'0.
18.1
'
;
protected
$version
=
'0.
20.2
'
;
/**
/**
* Telegram API key
* Telegram API key
...
@@ -81,6 +81,23 @@ class Telegram
...
@@ -81,6 +81,23 @@ class Telegram
*/
*/
protected
$log_path
;
protected
$log_path
;
/**
* Upload Path
*
* @var string
*/
protected
$upload_path
;
/**
* Dowload Path
*
* @var string
*/
protected
$download_path
;
/**
/**
* Log verbosity
* Log verbosity
*
*
...
@@ -117,6 +134,7 @@ class Telegram
...
@@ -117,6 +134,7 @@ class Telegram
protected
$message_types
=
array
(
'text'
,
'command'
,
'new_chat_participant'
,
protected
$message_types
=
array
(
'text'
,
'command'
,
'new_chat_participant'
,
'left_chat_participant'
,
'new_chat_title'
,
'delete_chat_photo'
,
'group_chat_created'
'left_chat_participant'
,
'new_chat_title'
,
'delete_chat_photo'
,
'group_chat_created'
);
);
/**
/**
* Admins List
* Admins List
*
*
...
@@ -133,11 +151,13 @@ class Telegram
...
@@ -133,11 +151,13 @@ class Telegram
protected
$admin_enabled
=
false
;
protected
$admin_enabled
=
false
;
/**
/**
* Constructor
* Constructor
*
*
* @param string $api_key
* @param string $api_key
*/
*/
public
function
__construct
(
$api_key
,
$bot_name
)
public
function
__construct
(
$api_key
,
$bot_name
)
{
{
if
(
empty
(
$api_key
))
{
if
(
empty
(
$api_key
))
{
...
@@ -348,6 +368,7 @@ class Telegram
...
@@ -348,6 +368,7 @@ class Telegram
print
(
date
(
'Y-m-d H:i:s'
,
time
())
.
' - Processed '
.
$a
.
" updates
\n
"
);
print
(
date
(
'Y-m-d H:i:s'
,
time
())
.
' - Processed '
.
$a
.
" 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
())
.
" - Fail fetch updates
\n
"
);
echo
$ServerResponse
->
printError
();
}
}
//return $results
//return $results
...
@@ -566,6 +587,54 @@ class Telegram
...
@@ -566,6 +587,54 @@ class Telegram
}
}
/**
* Set custom upload path
*
* @return object
*/
public
function
setUploadPath
(
$folder
)
{
if
(
!
is_dir
(
$folder
))
{
throw
new
TelegramException
(
'Upload folder not exists!'
);
}
$this
->
upload_path
=
$folder
;
return
$this
;
}
/**
* Get custom upload path
*
* @return string
*/
public
function
getUploadPath
(
$folder
)
{
return
$this
->
upload_path
;
}
/**
* Set custom Download path
*
* @return object
*/
public
function
setDownloadPath
(
$folder
)
{
if
(
!
is_dir
(
$folder
))
{
throw
new
TelegramException
(
'Download folder not exists!'
);
}
$this
->
upload_path
=
$folder
;
return
$this
;
}
/**
* Get custom Download path
*
* @return string
*/
public
function
getDownloadPath
(
$folder
)
{
return
$this
->
download_path
;
}
/**
/**
* Set command config
* Set command config
*
*
...
...
tests/Unit/Entities/ServerResponseTest.php
View file @
5caebd47
...
@@ -224,6 +224,84 @@ class ServerResponseTest extends TestCase
...
@@ -224,6 +224,84 @@ class ServerResponseTest extends TestCase
$this
->
assertNull
(
$this
->
server
->
getResult
());
$this
->
assertNull
(
$this
->
server
->
getResult
());
}
}
/**
* @test
*/
public
function
getUserProfilePhotos
()
{
return
'{
"ok":true,
"result":{
"total_count":3,
"photos":[
[
{"file_id":"AgADBG6_vmQaVf3qOGVurBRzHqgg5uEju-8IBAAEC","file_size":7402,"width":160,"height":160},
{"file_id":"AgADBG6_vmQaVf3qOGVurBRzHWMuphij6_MIBAAEC","file_size":15882,"width":320,"height":320},
{"file_id":"AgADBG6_vmQaVf3qOGVurBRzHNWdpQ9jz_cIBAAEC","file_size":46680,"width":640,"height":640}
],
[
{"file_id":"AgADBAADr6cxG6_vmH-bksDdiYzAABO8UCGz_JLAAgI","file_size":7324,"width":160,"height":160},
{"file_id":"AgADBAADr6cxG6_vmH-bksDdiYzAABAlhB5Q_K0AAgI","file_size":15816,"width":320,"height":320},
{"file_id":"AgADBAADr6cxG6_vmH-bksDdiYzAABIIxOSHyayAAgI","file_size":46620,"width":640,"height":640}
],
[
{"file_id":"AgABxG6_vmQaL2X0CUTAABMhd1n2RLaRSj6cAAgI","file_size":2710,"width":160,"height":160},
{"file_id":"AgADcxG6_vmQaL2X0EUTAABPXm1og0O7qwjKcAAgI","file_size":11660,"width":320,"height":320},
{"file_id":"AgADxG6_vmQaL2X0CUTAABMOtcfUmoPrcjacAAgI","file_size":37150,"width":640,"height":640}
]
]
}
}'
;
}
public
function
testGetUserProfilePhotos
()
{
$result
=
$this
->
getUserProfilePhotos
();
$this
->
server
=
new
ServerResponse
(
json_decode
(
$result
,
true
),
'testbot'
);
$this
->
assertCount
(
3
,
$this
->
server
->
getResult
()
->
getPhotos
());
$this
->
assertCount
(
3
,
$this
->
server
->
getResult
()
->
getPhotos
()[
0
]);
$this
->
assertInstanceOf
(
'\Longman\TelegramBot\Entities\UserProfilePhotos'
,
$this
->
server
->
getResult
());
$this
->
assertInstanceOf
(
'\Longman\TelegramBot\Entities\PhotoSize'
,
$this
->
server
->
getResult
()
->
getPhotos
()[
0
][
0
]);
}
/**
* @test
*/
public
function
getFile
()
{
return
'{
"ok":true,
"result":{
"file_id":"AgADBxG6_vmQaVf3qRzHYTAABD1hNWdpQ9qz_cIBAAEC",
"file_size":46680,
"file_path":"photo\/file_1.jpg"
}
}'
;
}
public
function
testGetFile
()
{
$result
=
$this
->
getFile
();
//print_r(json_decode($result, true));
$this
->
server
=
new
ServerResponse
(
json_decode
(
$result
,
true
),
'testbot'
);
//var_dump($this->server->getResult()->getPhotos());
$this
->
assertInstanceOf
(
'\Longman\TelegramBot\Entities\File'
,
$this
->
server
->
getResult
());
}
/**
/**
* @test
* @test
*/
*/
...
...
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