jakebathman
8/26/2015 - 8:56 PM

GroupMe bot post function including @mentions

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);
}