Skip to content

Instantly share code, notes, and snippets.

@jonpalmisc
Created April 22, 2022 21:17
Show Gist options
  • Save jonpalmisc/d937458f202209c3dd0ceeb474e9baa6 to your computer and use it in GitHub Desktop.
Save jonpalmisc/d937458f202209c3dd0ceeb474e9baa6 to your computer and use it in GitHub Desktop.
GroupMe chat archiver
<?php
/**
* Chat archiver for GroupMe.
*
* Depends on <https://github.com/jspaetzel/GroupMePHP>.
*
* Copyright (c) 2022 Jon Palmisciano
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
require './vendor/autoload.php';
$client = new \GroupMePHP\Client('API_KEY');
$msg_svc = new \GroupMePHP\Services\MessagesService($client);
$group_id = 1000000;
$all_msgs = [];
$before_id = 0;
while (true) {
$args = [];
if ($before_id != 0) {
$args = ["before_id" => $before_id];
}
// Make the next request, abort on error.
$res_json = $msg_svc->index($group_id, $args);
$res = json_decode($res_json, false);
if (is_null($res)) {
break;
}
// Abort on non-success status code.
$ok = $res->meta->code == 200;
if ($ok == false) {
break;
}
// Get all messages and the last message.
$msgs = $res->response->messages;
$last = end($msgs);
reset($msgs);
// Update the "before_id" parameter.
$before_id = $last->id;
// Add all the messages to the combined message list.
$tmp = array_merge($all_msgs, $msgs);
$all_msgs = $tmp;
}
// Put the messages in the correct order, then print them all.
$history = array_reverse($all_msgs);
for ($i = 0; $i < count($history); $i++) {
print(date('c', $history[$i]->created_at) . " <" . $history[$i]->name . "> " . $history[$i]->text . "\n");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment