Пример #1
0
 * @license https://github.com/chomado/chomado_bot/blob/master/LICENSE MIT
 */
use Abraham\TwitterOAuth\TwitterOAuth;
use chomado\bot\Config;
use chomado\bot\DateTime as MyDateTime;
use chomado\bot\Log;
use chomado\bot\RandomSentenceList;
use chomado\bot\TwitterUtil;
use chomado\bot\format\WeatherFormatter;
use chomado\bot\weather\yahoocom\Client as WeatherClient;
// bootstrap
require_once __DIR__ . '/vendor/autoload.php';
Log::setErrorHandler();
// ファイルの行をランダムに抽出
$randomComments = new RandomSentenceList(__DIR__ . '/tweet_content_data_list/list.txt');
Log::trace("list.txtは" . count($randomComments) . "行です");
$timeZoneJst = new DateTimeZone('Asia/Tokyo');
$now = new MyDateTime('now', $timeZoneJst);
// 天気情報
$weather = (new WeatherClient('tokyo'))->query();
$formattedWeather = WeatherFormatter::formatForWeatherTweet($weather, '東京', $timeZoneJst);
// 呟く文成形
// ============================================================================
// (*゚▽゚* っ)З ちょまぎょ!
//
// 現在時刻は17:55です。
// 東京の17:30現在の天気は、にわか雨(7.2℃)です。
// 明日はところにより曇り(昼)で、最高気温12.8℃、最低気温3.3℃です。
// ============================================================================
$message = sprintf("%s\n\n%s\n%s", $randomComments->get(), '現在時刻は' . $now->format('H:i') . 'です。', $formattedWeather);
// Twitterに接続
Пример #2
0
Log::info("Twitter に問い合わせます。\nパラメータ:");
Log::info($param);
$res = $connection->get('statuses/mentions_timeline', $param);
if (!is_array($res)) {
    Log::error("Twitter から配列以外が返却されました:");
    Log::error($res);
    exit(1);
}
if (empty($res)) {
    Log::success("新着はありません");
    exit(0);
}
Log::success("Twitter からメンション一覧を取得しました。新着は " . count($res) . " 件です。");
// 最終投稿IDを書き込む
file_put_contents(__DIR__ . '/runtime/last_id.txt', $res[0]->id_str);
Log::trace("最終投稿IDを保存しました: " . $res[0]->id_str);
$success_count = 0;
$failure_count = 0;
$chat_context_manager = new ChatContextManager();
foreach ($res as $re) {
    $param = [];
    Log::info("届いたメッセージ:");
    Log::info(sprintf("    [@%s] %s - %s\n", $re->user->screen_name, $re->user->name, $re->text));
    // もし自分自身宛てだったら無視する.(無限ループになっちゃうから)
    if (strtolower($re->user->screen_name) === strtolower($config->getTwitterScreenName())) {
        Log::info("投稿ユーザが自分なので無視します");
        continue;
    }
    // リプライ本文から余計なものを取り除く.
    // 例: "@chomado_bot こんにちは" → "こんにちは"
    $text = trim(preg_replace('/@[a-z0-9_]+/i', '', $re->text));