My Computer · 2024/01/16 0

通过RSS订阅圈养一只长毛象mastodon机器人

站里有点冷清,所以打算养几个机器人,最简单的方法是通过MastoFeed ,绑定实例账号后 Create a new feed. 实测添加后到发布第一条消息用了很长时间,无法确定和定义发文时间。

我使用的来自rsshub的订阅源,内容会带一些html代码,在mastodon发布以后会显示源码,所以只能通过程序把内容再转换一下,把这个php的网址放到MastoFeed里就可以了:

<?php

// 获取 RSS 订阅内容,取5条内容,200字符
$url = "https://rsshub.app/36kr/information?limit=5&brief=200";
$rss = simplexml_load_file($url);

// 生成 RSS 头部
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rss version=\"2.0\">
<channel>
    <title>36氪 RSS 订阅</title>
    <link>https://36kr.com</link>
    <description>36氪最新资讯</description>
    <language>zh-cn</language>
";

// 循环遍历 RSS 订阅内容
foreach ($rss->channel->item as $item) {

    // 获取 title 和 description 内容
    $title = $item->title;
    $description = $item->description;

    // 去除 description 部分内容的 HTML 代码
    $description = strip_tags($description);

    // 生成 RSS 条目
    $xml .= "
        <item>
            <title>$title</title>
            <description>$description</description>
            <link>$item->link</link>
            <pubDate>$item->pubDate</pubDate>
        </item>
    ";
}

// 生成 RSS 尾部
$xml .= "
</channel>
</rss>
";

// 输出 RSS 订阅内容
echo $xml;

?>

第二个方法:使用pipedream 建立工作流来自动发布嘟文;

可以白嫖,自定义项很多,免费版支持3用户、3工作流、每天25条消息。

大概步骤:新建项目 - 新建工作流 - 添加trigger - New Item in Feed - convert_html_to_text - post_status

好用,就是免费的发布条数太少了。。。

第三个方法:php机器人

mastodon-rss-bot : 一个简单的 php 脚本,用于从 RSS/Atom 提要中检索最新项目并发布到 Mastodon 帐户

$token="你的访问令牌"; 
// Token of your Mastodon bot account

$base_url="https://mastodon网址"; 
// URL of your instance (Do not include '/' at the end.)

$feed_url="https://rsshub.app/36kr/information?brief=200"; 
// URL of RSS or Atom feed

$post = strip_tags($rss->item->title) . "\n\n" . strip_tags($rss->item->description) . "\n\n" . "全文:" . strip_tags($rss->item->link);
// 定义输出格式

简单修改以上几个参数,设置定时访问bot.php文件网址就实现自动发文了。在php7433环境下,发布日志记录和文章公开频率都是无效的,需要进行适当调整。

这里还有一个用php发布图片和文章的代码:GitHub - ulrischa/MastodonPoster: Simple PHP class to post status with images on mastodon

两个例子整合一下,既能发布文字也能上传图片,我就是这么做的 😄

对于没有rss的网站只能自己动手了,现在有了各种GPT的加持,只要经过细心的调教最后都能得到自己想要的结果。

相关文章