Steamer Lane Studio技術備忘録ム―バブルタイプ

MovableType cronジョブによる投稿記事のtwitter自動投稿bot

movable-type MovableType cronジョブによる投稿記事のtwitter自動投稿bot
最終更新日: 2023年4月15日

今まではposttweet等plを使っていたが、7/4頃から使えなくなった。
恐らく、twitter側に仕様変更だとは思うが、これはちょっと困るので別の方法を検索。dlvrなどのサービスは嫌だったので色々さがした結果、dlvrと手法は似ているが自前でそのシステムを組める記事を発見、MT環境での構築を考えた。

「twitterOAuthを利用したphpによるtwitterへの投稿。」
twitterOAuthをダウンロードし解凍、それ用のディレクトリへアップ。
次いでカスタムテンプレートでツイート、アカウントプロフィールを取得するphpファイルを作る。設置は先ほどアップしたtwitterOAuthファイルと同じディレクトリへ作られる様に設定。

<?php
//ライブラリの読み込み
require_once("twitteroauth/autoload.php");
require_once('twitteroauth/src/TwitterOAuth.php');
use Abraham\TwitterOAuth\TwitterOAuth;

$consumerKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
$consumerSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
$accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
$accessTokenSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';

$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
//日別アーカイブでの設定になっているので、エントリーアーカイブの際は要修正
$text = array(
"<mt:MultiBlog include_blogs="4"><MTArchiveList archive_type="Daily" lastn="1"><MTEntries glue="・" sort_order="ascend"><MTEntryTitle></MTEntries> <$mt:ArchiveLink encode_html="1"$></MTArchiveList></mt:MultiBlog>",
);

foreach ($text as $value) {
$re = $connection->post("statuses/update", array(
"status" => $value,
));
}

基本、OGPとtwitter cardを利用することで画像の表示等を自動化。
※coreserverではphpの初期バージョンが53だった。54以上に変更が必要。

cronジョブで叩く
後はcronで前述phpファイルを叩く様設定をすればOK。設定は、サーバーに依るので、そのサーバーでのやり方で。

ところが・・・・・twitterの仕様上同じツイートを連続することは出来ないのだが、そのくくりは回数か時間かで、(例えば)10数時間後にはcronジョブにより同じ投稿がツイートされてしまった。
頻繁にツイートするのであればcronの時間調整で問題ないが、1日一投などではツイートがダブってしまう。

そこで色々対策を探したが、基本このbotはある程度頻繁にツイートすることを考えてのことらしく、連続投稿を可能にするといった対策ばかりが目につく。
そこで結局、phpで組んだ日時制限にMTで投稿日時を呼び出し、その時間以降はcronで叩かれてもphpによる自動投稿が動作しない様にした。

準備
先ず、twitterOAuthでは”use~”部分でエラーが出るのでTwistOAuthを利用する。使い方はtwitterOAuthと同じ。
で、MTに生成させるコード

<?php
ini_set("display_errors", On);
error_reporting(E_ALL);
?>
<?php if (date('Y-m-d H:i:s') < '<mt:MultiBlog include_blogs="4"><MTArchiveList archive_type="Daily" lastn="1"><MTEntries glue="・" sort_order="descend"><MTEntryDate format="%Y">-<MTEntryDate format="%m">-<MTEntryDate format="%d"> <MTEntryDate format="%H" setvar="hour"><mt:Var name="hour" value="1" op="+">:<MTEntryDate format="%M">:00</MTEntries></MTArchiveList></mt:MultiBlog>'): ?>
<?php
require ('TwistOAuth/build/TwistOAuth.phar');
$ck = 'xxxxxxxx';
$cs = 'xxxxxxxx';
$at = 'xxxxxxxx';
$as = 'xxxxxxxx';
try {
$to = new TwistOAuth($ck, $cs, $at, $as);
$status = $to->post('statuses/update', ['status' => '<mt:MultiBlog include_blogs="4"><MTArchiveList archive_type="Daily" lastn="1"><MTEntries glue="・" sort_order="ascend"><MTEntryTitle></MTEntries> <$mt:ArchiveLink encode_html="1"$></MTArchiveList></mt:MultiBlog>']);
var_dump($status);
} catch (TwistException $e) {
echo "[{$e->getCode()}] {$e->getMessage()}";
}
?>
<?php endif; ?>

最初の4行は、500エラーが出た際にエラーを返すためのコードなので不要。
5行目で日時制限=投稿(entrydate、いわゆる通常の公開日)から1時間後以降は6行目以降が消されるphpスクリプト生成。phpは更に詳しくないのでつたないコードだが、<mt:entrydate format=”%H” setvar=”hour”><mt:Var name=”hour” value=”1″ op=”+”>で1時間後を設定、cronジョブの間隔との兼ね合いで要変更。
これで連続投稿する際はcronジョブの間隔だけ空けなければいかないが、頻繁に投稿をしない場合のbotができた。