Skip to content

Commit 21baa31

Browse files
committed
Minor changes
- Fixes code sample and typo in README - Adds helper methods to easily send emojis
1 parent 66d5716 commit 21baa31

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Install using Composer:
66
`"emilh91/groupme-api-client": "dev-master"`
77

88
### Boilerplate
9-
Your GroupMe API key can be found on the page mentioned above once you are logged in.
9+
Your GroupMe API key can be found on the page mentioned above once you are logged in. You do not need to specify an API key if you only plan to use this library for **sending bot messages**.
1010
```php
1111
require 'vendor/autoload.php';
1212
$c = new GroupMeApi\Client('API-KEY');
@@ -20,6 +20,7 @@ All the methods in the following sub-sections should be invoked on the newly cre
2020
public function getMyBots()
2121
public function createBot($bot_name, $group_id, $avatar_url='', $callback_url='')
2222
public function sendBotMessage($bot_id, $text, array $attachments=array())
23+
public function parseBotMessage($bot_id, $text)
2324
public function destroyBot($bot_id)
2425
```
2526

@@ -29,7 +30,8 @@ public function getDirectMessageChats($page=1, $per_page=10)
2930
public function getLatestDirectMessages($other_user_id, $limit=20)
3031
public function getDirectMessagesBefore($other_user_id, $message_id)
3132
public function getDirectMessagesSince($other_user_id, $message_id)
32-
public function sendDirectMessage($other_user_id, $text, $source_guid=null, array $attachments=array())
33+
public function sendDirectMessage($other_user_id, $text, array $attachments=array(), $source_guid=null)
34+
public function parseDirectMessage($other_user_id, $text, $source_guid=null)
3335
public function likeDirectMessage($other_user_id, $message_id)
3436
public function unlikeDirectMessage($other_user_id, $message_id)
3537
```
@@ -42,7 +44,7 @@ public function getGroupByName($name)
4244
public function getGroupById($group_id)
4345
public function getFormerGroups()
4446
public function createGroup($name, $description='', $image_url='', $share=false)
45-
public function getGroupDetails($group_id)
47+
public function getGroupDetails($group)
4648
public function updateGroupDetails($group_id, array $payload)
4749
public function destroyGroup($group_id)
4850
public function joinGroup($group_id, $share_token)
@@ -62,10 +64,11 @@ public function getLatestGroupMessages($group_id, $limit=20)
6264
public function getGroupMessagesBefore($group_id, $message_id, $limit=20)
6365
public function getGroupMessagesAfter($group_id, $message_id, $limit=20)
6466
public function getGroupMessagesSince($group_id, $message_id, $limit=20)
65-
public function getGroupNameById($id)
66-
public function getGroupIdByName($name)
67-
public function isMemberOfGroup($grp)
67+
public function getGroupNameById($group_id)
68+
public function getGroupIdByName($group_name)
69+
public function isMemberOfGroup($group_id)
6870
public function sendGroupMessage($group_id, $text, array $attachments=array(), $source_guid=null)
71+
public function parseGroupMessage($group_id, $text, $source_guid=null)
6972
```
7073

7174
##### User methods
@@ -87,27 +90,24 @@ public static function makeEmojiAttachment(array $charmap)
8790
```
8891

8992
### Emojis
90-
Aah, the pinnacle of modern communication... To send emojis in GroupMe, you need to specify a charmap (character map) when creating the attachment. For this purpose, another factory class exists: `GroupMeApi\EmojiUtils`.
93+
Aah, the pinnacle of modern communication... ~~To send emojis in GroupMe, you need to specify a charmap (character map) when creating the attachment. For this purpose, another factory class exists: `GroupMeApi\EmojiUtils`.~~ To send emojis without dealing with charmaps and placeholders, just write them inline and use the `parse*Message(...)` methods to send them.
9194

9295
```php
9396
require 'vendor/autoload.php';
9497
$c = new GroupMeApi\Client('API-KEY');
95-
96-
$raw_text = 'Hello :cool_guy_face::cigar_face:';
97-
$emojification = GroupMeApi\EmojiUtils::extractEmojiNamesFromText($raw_text); // returns an array
98-
$emoji_attachment = GroupMeApi\AttachmentUtils::makeEmojiAttachment($emojification['charmap']);
99-
$c->sendDirectMessage('OTHER-USER-ID', $emojification['text'], null, array($emoji_attachment));
98+
$c->parseBotMessage('BOT-ID', 'Awaiting instructions... :frustrated_face:');
99+
$c->parseDirectMessage('OTHER-USER-ID', 'Hello :cool_guy_face::cigar_face:');
100+
$c->parseGroupMessage('GROUP-ID', 'Hello everyone! :smiley_face::content_face:');
100101
```
101102

102103
### Image Service
103-
Before using images in messages you have to upload an image to GroupMe's image service.
104+
Before using local images in messages you have to upload an image to GroupMe's image service.
104105

105106
```php
106107
require 'vendor/autoload.php';
107108
$c = new GroupMeApi\Client('API-KEY');
108109
$res = $c->uploadImage('my_image_file.png', 'image/png', 'testpic');
109110
```
110111

111-
If the upload was successfull, the return variable contains the image url in `$res['payload']['url']` or an error message in `$res['error'][]`.
112-
112+
If the upload was successful, the return variable contains the image url in `$res['payload']['url']` or an error message in `$res['error'][]`.
113113
Thanks to user [rgaida](https://github.com/rgaida) for fixing the image service!

src/Client.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Client {
1616
/**
1717
* Maximum length of group descriptions
1818
*/
19-
const MAX_GRP_DESC_LEN = 255;
19+
const MAX_GROUP_DESC_LEN = 255;
2020

2121
const HTTP_OK = 200;
2222
const HTTP_BAD_REQUEST = 400;
@@ -102,6 +102,20 @@ public function sendBotMessage($bot_id, $text, array $attachments=array()) {
102102
);
103103
return $this->post('/bots/post', $payload);
104104
}
105+
106+
/**
107+
* Parses the message text and then sends it from a bot
108+
*
109+
* @param int $bot_id Bot id
110+
* @param string $text Message text
111+
*
112+
* @return mixed
113+
*/
114+
public function parseBotMessage($bot_id, $text) {
115+
$emojification = GroupMeApi\EmojiUtils::extractEmojiNamesFromText($text);
116+
$emoji_attachment = GroupMeApi\AttachmentUtils::makeEmojiAttachment($emojification['charmap']);
117+
$this->sendBotMessage($bot_id, $emojification['text'], array($emoji_attachment));
118+
}
105119

106120
/**
107121
* Destroys a bot
@@ -231,6 +245,21 @@ public function sendDirectMessage($other_user_id, $text, array $attachments=arra
231245
$payload = array('direct_message' => $message_info);
232246
return $this->post('/direct_messages', $payload);
233247
}
248+
249+
/**
250+
* Parses the message text and then sends it to another user
251+
*
252+
* @param int $other_user_id The other participant
253+
* @param string $text Message text
254+
* @param string $source_guid Unique id
255+
*
256+
* @return mixed
257+
*/
258+
public function parseDirectMessage($other_user_id, $text, $source_guid=null) {
259+
$emojification = GroupMeApi\EmojiUtils::extractEmojiNamesFromText($text);
260+
$emoji_attachment = GroupMeApi\AttachmentUtils::makeEmojiAttachment($emojification['charmap']);
261+
$this->sendDirectMessage($other_user_id, $emojification['text'], array($emoji_attachment), $source_guid);
262+
}
234263

235264
/**
236265
* Likes a message
@@ -264,21 +293,21 @@ public function unlikeDirectMessage($other_user_id, $message_id) {
264293
* Checks if the authenticated user is a member
265294
* of a certain group
266295
*
267-
* @param mixed $grp Group name or group id
296+
* @param mixed $group Group name or group id
268297
*
269298
* @return bool
270299
*/
271-
public function isMemberOfGroup($grp) {
300+
public function isMemberOfGroup($group) {
272301
$res = $this->getAllGroups();
273302

274303
if($res['meta']['code'] == self::HTTP_OK) {
275-
foreach($res['response'] as $group) {
276-
if ((is_numeric($grp) && $group['id'] == $grp) ||
277-
(is_string($grp) && $group['name'] == $grp)) return TRUE;
304+
foreach($res['response'] as $g) {
305+
if ((is_numeric($group) && $g['id'] == $group) ||
306+
(is_string($group) && $g['name'] == $group)) return true;
278307
}
279308
}
280309

281-
return FALSE;
310+
return false;
282311
}
283312

284313
/**
@@ -399,7 +428,7 @@ public function createGroup($name, $description='', $image_url='', $share=false)
399428
self::MAX_PRI_NAME_LEN) ? strlen($name) : self::MAX_PRI_NAME_LEN),
400429

401430
'description' => substr($description, 0, (strlen($description) <=
402-
self::MAX_GRP_DESC_LEN) ? strlen($description) : self::MAX_GRP_DESC_LEN),
431+
self::MAX_GROUP_DESC_LEN) ? strlen($description) : self::MAX_GROUP_DESC_LEN),
403432

404433
'image_url' => $image_url,
405434
'share' => boolval($share)
@@ -711,8 +740,8 @@ public function getGroupMessagesSince($group_id, $message_id, $limit=20) {
711740
*
712741
* @return mixed
713742
*/
714-
public function sendGroupMessage($group_id, $text, $source_guid=null,
715-
array $attachments=array()) {
743+
public function sendGroupMessage($group_id, $text, array $attachments=array(),
744+
$source_guid=null) {
716745

717746
$message_info = array(
718747
'text' => $text,
@@ -722,6 +751,21 @@ public function sendGroupMessage($group_id, $text, $source_guid=null,
722751
$payload = array('message' => $message_info);
723752
return $this->post("/groups/$group_id/messages", $payload);
724753
}
754+
755+
/**
756+
* Parses the message text and then sends it to a group
757+
*
758+
* @param int $group_id Group id
759+
* @param string $text Message text
760+
* @param string $source_guid Unique id
761+
*
762+
* @return mixed
763+
*/
764+
public function parseGroupMessage($group_id, $text, $source_guid=null) {
765+
$emojification = GroupMeApi\EmojiUtils::extractEmojiNamesFromText($text);
766+
$emoji_attachment = GroupMeApi\AttachmentUtils::makeEmojiAttachment($emojification['charmap']);
767+
$this->sendGroupMessage($group_id, $emojification['text'], array($emoji_attachment), $source_guid);
768+
}
725769

726770
// USER METHODS
727771

0 commit comments

Comments
 (0)