2013-10-11 14 views
8

Trên Twitter, bạn chỉ có thể tạo các bài đăng phù hợp với 140 ký tự. Điều đó bao gồm các tay cầm bạn đang tweet tại: @handle. Điều này tạo ra một vấn đề khi cố gắng tweet tại một nhóm lớn hơn. Trong một nỗ lực để tạo ra một workaround cho đội bóng rổ của tôi, tôi đang cố gắng để tạo ra một twitter bot rằng khi tweeted tại, nó lấy văn bản từ tweet và sau đó gửi một loạt các tweets @ xử lý của mỗi người trong đội.TweetBot Retweeter không cấp quyền cho phép

Tôi bắt đầu bằng mã từ this tutorial. Sau đó, tôi thay đổi nội dung ra những thứ Wolfram Alpha và đã đưa ra đoạn mã này để bắt đầu với: (Điều quan trọng và bí mật arent thực sự xxxxx)

/**  ScotsTeamRetweeter        **/ 
/**  =======================================   **/ 
/**  Written by John Holland  
/**  Taken from: Amit Agarwal @labnol on 10/09/2013 **/ 
/**  Tutorial link: http://www.labnol.org/?p=27902 **/ 

function start() {  
    Logger.log("Did actually start"); 

    // REPLACE THESE DUMMY VALUES  
    var TWITTER_CONSUMER_KEY  = "XXXXXX"; 
    var TWITTER_CONSUMER_SECRET = "XXXXXX"; 
    var TWITTER_HANDLE   = "ScotsTeam"; 

    // Store variables 
    ScriptProperties.setProperty("TWITTER_CONSUMER_KEY", TWITTER_CONSUMER_KEY); 
    ScriptProperties.setProperty("TWITTER_CONSUMER_SECRET", TWITTER_CONSUMER_SECRET); 
    ScriptProperties.setProperty("TWITTER_HANDLE",   TWITTER_HANDLE); 
    ScriptProperties.setProperty("MAX_TWITTER_ID",   0); 

    // Delete exiting triggers, if any 
    var triggers = ScriptApp.getScriptTriggers(); 
    for(var i=0; i < triggers.length; i++) { 
    ScriptApp.deleteTrigger(triggers[i]); 
    } 

    // Setup trigger to read Tweets every five minutes 
    ScriptApp.newTrigger("fetchTweets") 
      .timeBased() 
      .everyMinutes(1) 
      .create(); 
} 

function oAuth() { 
    var oauthConfig = UrlFetchApp.addOAuthService("twitter"); 
    oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token"); 
    oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token"); 
    oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize"); 
    oauthConfig.setConsumerKey(ScriptProperties.getProperty("TWITTER_CONSUMER_KEY")); 
    oauthConfig.setConsumerSecret(ScriptProperties.getProperty("TWITTER_CONSUMER_SECRET")); 
} 

function fetchTweets() { 
    oAuth(); 
    var twitter_handle = ScriptProperties.getProperty("TWITTER_HANDLE"); 

    var phrase = "lang:en+to:" + twitter_handle; 
    var search = "https://api.twitter.com/1.1/search/tweets.json?count=5&include_entities=false&result_type=recent&q="; 
    search = search + encodeString(phrase) + "&since_id=" + ScriptProperties.getProperty("MAX_TWITTER_ID");  

    var options = 
    { 
    "method": "get", 
    "oAuthServiceName":"twitter", 
    "oAuthUseToken":"always" 
    }; 

    try { 

    var result = UrlFetchApp.fetch(search, options);  
    if (result.getResponseCode() === 200) { 
     var data = Utilities.jsonParse(result.getContentText()); 
     if (data) { 
     var tweets = data.statuses; 
     for (var i=tweets.length-1; i>=0; i--) { 
      var question = tweets[i].text.replace(new RegExp("\@" + twitter_handle, "ig"), "");    
      var answer = "Looks Like it worked" 
      sendTweet(tweets[i].user.screen_name, tweets[i].id_str, answer); 
      Logger.log("Tweet should have sent");   
     } 
     } 
    } 
    } catch (e) { 
    Logger.log(e.toString()); 
    } 
} 

function sendTweet(user, reply_id, tweet) { 

    var options = 
    { 
    "method": "POST", 
    "oAuthServiceName":"twitter", 
    "oAuthUseToken":"always"  
    }; 

    var status = "https://api.twitter.com/1.1/statuses/update.json"; 

    status = status + "?status=" + encodeString("@" + user + " " + tweet); 
    status = status + "&in_reply_to_status_id=" + reply_id; 

    try { 
    var result = UrlFetchApp.fetch(status, options); 
    ScriptProperties.setProperty("MAX_TWITTER_ID", reply_id); 
    Logger.log(result.getContentText());  
    } 
    catch (e) { 
    Logger.log(e.toString()); 
    } 
} 

function encodeString (q) { 
    // Update: 09/06/2013 
    // Google Apps Script is having issues storing oAuth tokens with the Twitter API 1.1 due to some encoding issues. 
    // Hence this workaround to remove all the problematic characters from the status message. 

    var str = q.replace(/\(/g,'{').replace(/\)/g,'}').replace(/\[/g,'{').replace(/\]/g,'}').replace(/\!/g, '|').replace(/\*/g, 'x').replace(/\'/g, ''); 
    return encodeURIComponent(str); 

// var str = encodeURIComponent(q); 
// str = str.replace(/!/g,'%21'); 
// str = str.replace(/\*/g,'%2A'); 
// str = str.replace(/\(/g,'%28'); 
// str = str.replace(/\)/g,'%29'); 
// str = str.replace(/'/g,'%27'); 
// return str; 

} 

(sự hiểu biết của tôi là) Mã này nên chỉ gây bot twitter để viết tweet có nội dung "có vẻ như đã hoạt động" khi được tweet tại.

Tuy nhiên tôi dường như có vấn đề về ủy quyền mà tôi không hiểu. Tôi nhận được email này hầu hết các đêm:

Your script, ScotsTeamRetweeter, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here. 

Summary: 

Error Message Count 
Authorization is required to perform that action. 18 
Details: 

Start Function Error Message Trigger End 
10/9/13 9:11 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:11 PM 
10/9/13 9:12 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:12 PM 
10/9/13 9:13 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:13 PM 
10/9/13 9:14 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:14 PM 
10/9/13 9:15 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:15 PM 
10/9/13 9:16 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:16 PM 
10/9/13 9:17 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:17 PM 
10/9/13 9:18 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:18 PM 
10/9/13 9:19 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:19 PM 
10/9/13 9:20 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:20 PM 
10/9/13 9:21 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:21 PM 
10/9/13 9:22 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:22 PM 
10/9/13 9:23 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:23 PM 
10/9/13 9:24 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:24 PM 
10/9/13 9:25 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:25 PM 
10/9/13 9:26 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:26 PM 
10/9/13 9:27 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:27 PM 
10/9/13 9:28 PM fetchTweets Authorization is required to perform that action. time-based 10/9/13 9:28 PM 

Tôi nên làm gì để sửa lỗi bot của mình?

+0

không hoạt động sau khi gọi 'fetchTweets' theo cách thủ công và ủy quyền như @ br-araujo đề xuất? Btw, tôi khuyên bạn nên kiểm tra đầu tiên và nếu nó hoạt động, sau đó thiết lập kích hoạt ... xem cũng đăng nhập của bạn cho các lỗi ... btw. bạn hiện đã xác định trình kích hoạt đang chạy mỗi phút, có thể bạn muốn thay đổi điều đó thành 5 phút một lần thay thế? Trong dòng này 'var answer =" Trông giống như nó hoạt động "' một semikolon chấm dứt bị thiếu ... – Taifun

Trả lời

1

Bạn cần truy cập trình chỉnh sửa tập lệnh và gọi trực tiếp chức năng của mình. (Nút Phát). Sau đó, nó sẽ hiển thị một màn hình ủy quyền cho bạn và sau đó mọi thứ sẽ ổn.

+0

Tôi đã làm điều đó và vẫn không có tiến bộ ... –

+0

Điều gì đã xảy ra, bạn đã nhấp vào nút phát chưa? –

Các vấn đề liên quan