This commit is contained in:
Jeff MacKinnon 2022-05-11 17:12:37 -03:00
parent 416c03746d
commit f10c3726da
5 changed files with 128 additions and 0 deletions

21
MastodonBotPHP/LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Gabriele Grillo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,4 +1,9 @@
<?php <?php
/* */
/* This is a clone from https://github.com/Eleirbag89/MastodonBotPHP */
/* Any changes that I made are minor, support their work. */
/* I didn't know how to properly nest/fork with git, so I cloned. */
/* */
class MastodonAPI class MastodonAPI
{ {

99
bot.php
View file

@ -0,0 +1,99 @@
<?php
include_once 'MastodonBotPHP/Mastodon.php';
/* The setup */
$token = 'NeA2lgL7EeKQHoYcYAhOdBKtI4e9kDlrqB5f-V4kEUI'; // Token of your Mastodon welcome bot account
$base_url = 'https://bluenoser.me'; // 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 = '@jeffsblogbot';
/* */
// 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;
}
?>

0
comment_record.txt Normal file
View file

3
readme.rst Normal file
View file

@ -0,0 +1,3 @@
Here is the Bot for your Mastodon Comments
==========================================