forked from chomado/chomado_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.php
149 lines (128 loc) · 4.76 KB
/
reply.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* @author chomado <chomado@gmail.com>
* @copyright 2015 by chomado <chomado@gmail.com>
* @license https://github.com/chomado/chomado_bot/blob/master/LICENSE MIT
*/
use Abraham\TwitterOAuth\TwitterOAuth;
use chomado\bot\Chat;
use chomado\bot\Config;
use chomado\bot\Log;
use chomado\bot\RandomSentenceList;
use chomado\bot\TwitterUtil;
use chomado\bot\chat\ContextManager as ChatContextManager;
// bootstrap
require_once(__DIR__ . '/vendor/autoload.php');
Log::setErrorHandler();
$param = [];
// 最終投稿IDを取得
Log::trace("last_idを読み込みます。");
if (@file_exists(__DIR__ . '/runtime/last_id.txt')) {
if ($since_id = file_get_contents(__DIR__ . '/runtime/last_id.txt')) {
$param['since_id'] = $since_id;
Log::info("since_id: {$since_id} を読み込みました。");
} else {
Log::warning(
"last_id.txtからデータが読み込めません。空のパラメータが送信されます。"
);
}
unset($since_id);
} else {
Log::warning("last_id.txtがありません。空のパラメータが送信されます。");
}
// ファイルの行をランダムに抽出
$randomFaces = new RandomSentenceList(__DIR__ . '/tweet_content_data_list/face_list.txt');
Log::trace("face_listは" . count($randomFaces) . "行です");
// Twitterに接続
$config = Config::getInstance();
$connection = new TwitterOAuth(
$config->getTwitterConsumerKey(),
$config->getTwitterConsumerSecret(),
$config->getTwitterAccessToken(),
$config->getTwitterAccessTokenSecret()
);
// リプライを取得
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));
// もし数字だけだったら素数判定処理をする
if (filter_var($text, FILTER_VALIDATE_INT)) {
$num = intval($text);
$message = sprintf('%d の次の素数は %s です。', $num, '[そのうち実装するよ!]');
} else {
// botに来たリプライに数字以外のものが含まれていたら
// 通常の雑談対話リプライをする
$chat = new Chat(
$config->getDocomoDialogueApiKey(),
$chat_context_manager->getContextId($re->user->screen_name),
$chat_context_manager->getMode($re->user->screen_name),
$re->user->name,
$text
);
$message = sprintf('%s %s%s', $chat->ResText(), $randomFaces->get(), PHP_EOL);
}
$param['status'] = sprintf(
"@%s %sさん\n%s",
$re->user->screen_name,
trim(preg_replace('!([@@##.]|://)!u', " $1 ", $re->user->name)),
$message
);
$param['in_reply_to_status_id'] = $re->id_str;
// 投稿
if (TwitterUtil::postTweet($connection, $param)) {
++$success_count;
} else {
++$failure_count;
}
$chat_context_manager->setContext(
$re->user->screen_name,
$chat->GetChatContextId(),
$chat->GetChatMode()
);
}
Log::log(
sprintf("処理が完了しました: 成功 %d 件、失敗 %d 件", $success_count, $failure_count),
$failure_count > 0 ? 'error' : 'success'
);
/**
* エラーがあった時に私に知らせる
*/
if ($failure_count > 0) {
$param = [];
$param['status'] = sprintf(
"@%s 処理が完了しました: 成功 %d 件、失敗 %d 件",
$config->getTwitterOwnerScreenName(),
$success_count,
$failure_count
);
TwitterUtil::postTweet($connection, $param);
}
exit($failure_count > 0 ? 1 : 0);