Last active
November 10, 2022 14:17
-
-
Save fddcddhdd/f9f1418d23fbc9e19c57812fb232e1dc to your computer and use it in GitHub Desktop.
オウム返し返信をしてくれるtwitterボットをnode.jsで作ってみた。 ツイートに@相手のユーザIDを含ませるだけでは返信にならないので、パラメータに相手のツイートIDを指定する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// twitterモジュールを読み込み( npm install twitter ) | |
var twitter = require('twitter'); | |
// アプリ登録時に取得したkeyを入れてOAuth認証し、初期化 | |
var client = new twitter({ | |
consumer_key: 'xxxx', | |
consumer_secret: 'xxxxx', | |
access_token_key: 'xxxxx', | |
access_token_secret: 'xxxxx' | |
}); | |
// Public APIのstatuses/filterで取得したタイムラインを、自分のアカウント名を含む文字列でフィルターする | |
client.stream( 'statuses/filter', { track : '@ボットのユーザID' }, function( stream ) { | |
// フィルターされたデータのストリームを受け取り、ツイートのテキストを表示する | |
stream.on( 'data', function( data ) { | |
var text = data.text; // ツイートのテキスト | |
var textCleaned = '@' + data.user.screen_name + text.replace( /@ボットのユーザID/g, "" ); // アカウント名は不要 | |
console.log( textCleaned ); | |
// 相手に対してオウム返しの返信投稿(パラメータにツイートIDを指定する。文字列型でないとダメみたい…。) | |
client.post( | |
'statuses/update', {status: textCleaned, in_reply_to_status_id: data.id_str}, | |
function(error, tweet, response) { | |
if (!error) { | |
console.log("ok, reply.") | |
} | |
} | |
) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment