MastodonCommenting-pelican/bot.php
2022-05-11 17:22:27 -03:00

99 lines
No EOL
2.7 KiB
PHP

<?php
include_once 'MastodonBotPHP/Mastodon.php';
/* The setup */
$token = 'addtokenhere'; // Token of your Mastodon welcome bot account
$base_url = 'instanceURL'; // URL of your instance (Do not include '/' at the end.)
$visibility = 'public'; // "Direct" means sending welcome message as a private message. The four tiers of visibility for toots are Public , Unlisted, Private, and Direct (default)
$language = 'en'; // en for English, zh for Chinese, etc.
$instance_user = '@useryou are using'; // @user
/* */
// Functions
/* */
// This function looks for the URL in a CSV file and returns the result or false.
function find_status($filename, $url) {
$f = fopen($filename, "r");
$result = false;
while ($row = fgetcsv($f)) {
if ($row[0] == $url) {
$result = $row[1];
break;
}
}
fclose($f);
return $result;
}
// Step 0 - Create the site URL stuff
$comment_record = 'comment_record.txt';
$postTitle = $_GET['title'];
$postURL = $_GET['url'];
$postSummary = $_GET['summary'];
$postImage = $_GET['image'];
$postImage = str_replace("\\",'/',$postImage); // I need to do this because of a "thing" in the way that photos plugin creates the URL that has a strange slash
// Step 1 - Check to see if this URL is in the text file, if so send them to the Status id there.
$status_id = find_status($comment_record, $postURL);
if ($status_id != false) {
//301 redirect to the mastodon status
http_response_code(301);
header('Location: '.$base_url.'/'.$instance_user.'/'.$status_id);
exit;
} else {
// Step 2 - If the URL isn't in the text file, create a new status with the post URL stuff
$mastodon = new MastodonAPI($token, $base_url);
// Step 2a - upload the image
$curl_file = curl_file_create($postImage, 'image/jpg', 'imagename.jpg'); // I'm not sure what the last variable in this is for, I will figure this out later.
$body = [
'file' => $curl_file,
];
$response = $mastodon->uploadMedia($body);
$file_id = $response['id'];
// Step 2b - Create the status
$status_text = $postTitle. ' -> '.$postURL;
$status_data = [
'status' => $status_text,
'visibility' => $visibility,
'language' => $language,
'media_ids[]' => $file_id,
];
$status_response = $mastodon->postStatus($status_data);
// Step 3 - Update Text file
$status_id = $status_response['id'];
$f = fopen($comment_record, 'a');
fputcsv($f, [$postURL, $status_id]);
fclose($f);
// Step 4 - Send them to the post
// 301 redirect to the mastodon status
http_response_code(301);
header('Location: '.$base_url.'/'.$instance_user.'/'.$status_id);
exit;
}
?>