Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Created September 21, 2023 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ammarfaizi2/14d7d20ff8734be7500970c92b02783b to your computer and use it in GitHub Desktop.
Save ammarfaizi2/14d7d20ff8734be7500970c92b02783b to your computer and use it in GitHub Desktop.
<?php
ini_set("display_errors", true);
header("Content-Type: application/json");
require __DIR__."/../bootstrap/autoload.php";
loadConfig("api");
loadConfig("telegram_bot");
function get_messages(PDO $pdo, int $chat_id, ?int $last_tmsg_id = NULL,
?int $limit = NULL): ?array
{
if ($chat_id !== -1001483770714)
goto out_no_data;
if ($limit === NULL)
$limit = 100;
$st = $pdo->prepare(<<<SQL
SELECT
b.user_id,
b.username,
b.first_name,
b.last_name,
LOWER(CONCAT(
HEX(d.md5_sum),
"_",
HEX(d.sha1_sum),
IF(d.extension IS NULL, "", CONCAT(".", d.extension))
)) AS user_photo,
a.tmsg_id,
a.reply_to_tmsg_id,
a.msg_type,
a.text,
a.text_entities,
LOWER(CONCAT(
HEX(c.md5_sum),
"_",
HEX(c.sha1_sum),
IF(c.extension IS NULL, "", CONCAT(".", c.extension))
)) AS file,
a.tmsg_datetime AS dt
FROM
groups_messages AS a
INNER JOIN users AS b ON b.user_id = a.user_id
LEFT JOIN files AS c ON c.id = a.file
LEFT JOIN files AS d ON d.id = b.photo
WHERE
a.group_id = :chat_id AND
a.tmsg_id > :last_tmsg_id
ORDER BY a.created_at DESC LIMIT {$limit};
SQL);
$st->execute([
"chat_id" => $chat_id,
"last_tmsg_id" => is_int($last_tmsg_id) ?? 0
]);
$data = $st->fetchAll(PDO::FETCH_NUM);
goto out;
out_no_data:
$data = [];
out:
return [
"columns" => [
"user_id",
"username",
"first_name",
"last_name",
"user_photo",
"tmsg_id",
"reply_to_tmsg_id",
"msg_type",
"text",
"text_entities",
"file",
"dt"
],
"data" => $data,
];
}
function api_err(int $code, string $msg): array
{
return [
"success" => false,
"code" => $code,
"msg" => $msg
];
}
function api_get_messages(): array
{
$limit = NULL;
$last_tmsg_id = NULL;
if (!isset($_GET["chat_id"]) || !is_numeric($_GET["chat_id"]))
return api_err(400, "Missing a numeric \"chat_id\" parameter");
$chat_id = (int)$_GET["chat_id"];
if (isset($_GET["limit"])) {
if (!is_numeric($_GET["limit"]))
return api_err(400, "The \"limit\" parameter must be numeric");
$limit = (int)$_GET["limit"];
if ($limit < 1 || $limit > 300)
return api_err(400, "The \"limit\" parameter must be an integer in the range [1, 300]");
}
if (isset($_GET["last_tmsg_id"])) {
if (!is_numeric($_GET["last_tmsg_id"]))
return api_err(400, "The \"last_tmsg_id\" parameter must be numeric");
$last_tmsg_id = (int)$_GET["last_tmsg_id"];
}
$pdo = DB::pdo();
return [
"success" => true,
"code" => 200,
"result" => get_messages($pdo, $chat_id, $last_tmsg_id, $limit),
];
}
function main(): int
{
$res = [];
if (!isset($_GET["action"]) || !is_string($_GET["action"])) {
$res = api_err(400, "Invalid action");
goto out;
}
try {
switch ($_GET["action"]) {
case "get_messages":
$res = api_get_messages();
break;
default:
$res = api_err(400, "Invalid action");
break;
}
} catch (PDOException $e) {
$res = api_err(500, $e->getMessage());
} catch (Error $e) {
$res = api_err(500, $e->getMessage());
}
out:
http_response_code($res["code"] ?? 200);
echo json_encode($res, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return 0;
}
exit(main());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment