Skip to content

Instantly share code, notes, and snippets.

@jakebathman
Last active July 12, 2020 00:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jakebathman/0830789c9cd4167cd1da to your computer and use it in GitHub Desktop.
Save jakebathman/0830789c9cd4167cd1da to your computer and use it in GitHub Desktop.
GroupMe bot post function including @mentions
<?php
/**
* Post a message from a bot to a group
*
* $groupId (string) GroupId of the group to post the message
* $strMessage (string) Text of the message to post (limit of 1000 characters)
* $strBotId (string) ID of the bot which will post the message
* $mentionUsers (array) [optional] array of userIds to mention (uses attachment)
* Example array of userIds:
* ["123456","654321","987654"]
*
* @return Status 201 (Created)
* No return from this endpoint.
*
* This function, if used with @mentions, will a simmilar array to this:
*
* {
* "attachments": [
* {
* "loci": [
* [
* 10,
* 8
* ]
* ],
* "type": "mentions",
* "user_ids": [
* "12345678"
* ]
* }
* ],
* "botId": "16154809",
* "text": "Attention @person1!",
* }
*
*/
public function botPost($groupId, $strBotId, $strMessage, $mentionUsers = false)
{
if ((empty($mentionUsers) === false) && (is_array($mentionUsers) === true))
{
// Get current name of the user(s) to mention
// Start by getting all current group members
$r = $this->getGroup($groupId);
// Build a quick lookup array of the current group members (for use later)
foreach ($r['response']['members'] as $k => $v)
{
$arrMembersOfGroup[$v['user_id']] = $v['nickname'];
}
$arrUsersToMention = array();
$arrPostscriptNames = array();
foreach ($mentionUsers as $k => $v)
{
// Build flat array of @userName to make into string
if (in_array($v, array_keys($arrMembersOfGroup)))
{
if (strlen($k) > 3)
{
// Use the array keys as the text for the mention, instead of their current group nickname
$arrUsersToMention[] = "@" . $k;
}
else
{
$arrUsersToMention[] = "@" . $arrMembersOfGroup[$v];
}
$arrUserIds[] = $v;
}
else
{
// Not a member of the group, so add them to a postscript list
if (strlen($k) > 3)
{
$arrPostscriptNames[] = $k;
}
}
}
$strMentions = implode(" ", $arrUsersToMention);
if (count($arrPostscriptNames) > 0)
{
$strMentionsPostscript = ", and these people not in this group: " . implode(", ", $arrPostscriptNames);
}
$strMessage = $strMessage . " " . $strMentions . " " . $strMentionsPostscript;
$arrLoci = array();
foreach ($arrUsersToMention as $k => $v)
{
// Build the $arrLoci attachment, getting string position and lengths for mention replacement
$arrLoci[] = array(
stripos($strMessage, $v) ,
strlen($v)
);
}
$arrPayload["attachments"][] = array(
"loci" => $arrLoci,
"type" => "mentions",
"user_ids" => $arrUserIds
);
}
$arrPayload["bot_id"] = $strBotId;
$arrPayload["text"] = $strMessage;
$postUrl = $this->baseUrl . "/bots/post?token={$this->token}";
return $this->apiPost($postUrl, $arrPayload);
}
Copy link

ghost commented Jan 12, 2016

is there a way to modify this to mention all users in a group?, if so, can you please tell me how to do so?

@jakebathman
Copy link
Author

Nope! There are a few people that have thought about that, and it's not really supported (something like @everyone). You could use this to get the group members and loop over them, using an @mention for everyone individually. It's a bit clunky, but that's really the only practical way right now.

Copy link

ghost commented Jan 12, 2016

thats what i mean, I'm just wondering if you could tell me how to modify the code to mention everyone individually...

@rastalamm
Copy link

Any idea if this still works? Attempting to send the below but the user mentioned isn't added :(

data: {
    bot_id: botName,
    text: " - you've been mentioned!",
    attachments: [
        {
        type: "mentions",
        user_ids: [1234],
        loci: [[0, 0]],
        },
    ],
}

@jakebathman
Copy link
Author

Oh boy @rastalamm, I haven’t worked with this API in so long I wouldn’t be surprised if it had changed a bunch in the mean time. This was a really crappy API to work with if I remember correctly, and it’s possible they have a better way to structure the payload for a mention now.

@rastalamm
Copy link

Appreciate the quick response - feeling some of the pain you felt...

@jakebathman
Copy link
Author

I’d try getting the index of messages in a group first to see if there’s some different way that mention attachments are done: https://dev.groupme.com/docs/v3#messages_index

Past that: best of luck 🤷🏻‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment