Skip to content

Instantly share code, notes, and snippets.

@mntone
Created Nov 4, 2017
Embed
What would you like to do?
Google App Script の自動フォロー,DM script <This script is under The MIT License. | The MIT License にて>
'use strict';
// OAuth1認証用インスタンス
var twitter = TwitterWebService.getInstance(
'consumer_key',
'consumer_secret'
);
//OAuth1ライブラリを導入したうえで、getServiceを上書き
twitter.getService = function() {
return OAuth1.createService('Twitter2')
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
.setConsumerKey(twitter.consumer_key)
.setConsumerSecret(twitter.consumer_secret)
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
}
// 認証を行う(必須)
function authorize() {
var url = twitter.authorize();
Logger.log(url);
}
// 認証をリセット
function reset() {
twitter.reset();
}
// 認証後のコールバック(必須)
function authCallback(request) {
return twitter.authCallback(request);
}
function onFormSent(e) {
var itemResponses = e.response.getItemResponses();
var twitterId = '';
for (var i = 0; i < itemResponses.length; i++) {
var itemResponse = itemResponses[i];
var question = itemResponse.getItem().getTitle();
if (question.indexOf('Twitter') > -1) {
twitterId = itemResponse.getResponse();
}
}
Logger.log(twitterId);
follow(twitterId);
sentMessage(twitterId);
}
function follow(sn) {
var service = twitter.getService();
var formData = {
'screen_name': sn,
'follow': true,
};
var options = {
'method' : 'post',
'payload' : formData
};
var json = service.fetch('https://api.twitter.com/1.1/friendships/create.json', options);
var response = JSON.parse(json);
}
function sentMessage(sn) {
var service = twitter.getService();
var formData = {
'screen_name': sn,
'text': 'DM文',
};
var options = {
'method' : 'post',
'payload' : formData
};
var json = service.fetch('https://api.twitter.com/1.1/direct_messages/new.json', options);
var response = JSON.parse(json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment