LoginSignup
13
13

More than 5 years have passed since last update.

おやすみツイートの「おやあり」を自動的に返信

Last updated at Posted at 2017-06-08

やりたいこと

おやすみとツイート

おやすみ的なリプが来る

それに「おやあり」と自動的に返信する


ss.png
↑これを自動化したい

コード

ライブラリ

TwitterOAuth
↑これを使った

code.php
<?php
// OAuthライブラリの読み込み
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//認証情報
$consumerKey = "<Your consumer_key(API Key)>";
$consumerSecret = "<Your consumer_secret(API Secret)>";
$accessToken = "<Your access_token>";
$accessTokenSecret = "<Your access_token_secret>";
//自分のスクリーンネーム
$screen_name = "<Your screen_name>";

//接続
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);

//リプライを10件取得
$res = $connection->get("statuses/mentions_timeline", array("count" => "10"));

//リプライを取り出し
for ($i=0; $i < count($res); $i++) {
  //リプライにおやすみが含まれていたら
  if(strpos($res[$i]->text,'おやすみ') !== false){
    //自分のツイートじゃなかったら
    if ($res[$i]->user->screen_name !== $screen_name) {
      //おやありとリプ
      $rp = $connection-  >post("statuses/update", array("status" => "@".$res[$i]->user->screen_name."
    おやあり!","in_reply_to_status_id" => $res[$i]->id));
      //ファボる
      $fav = $connection->post("favorites/create", array("id" => $res[$i]->id));
    }
  }
}

↓※コメント欄に別のコードあり↓

CRON設定

CRONで2分ごとに実行
ss2.png
(さくらのレンタルサーバの例)

まとめ

これで、寝るって言ったけどおやありって返信しないといけないから寝れないみたいなことがなくなる。

13
13
8

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13