1

I've been playing around with the twitteR R package the past few days, and am running into a error using direct messaging. I wrote the following short script, basically just to see if I could effectively 'remote control' R by sending direct messages to an account it monitors and then executing whatever code was in the block corresponding to it, along with sending me a response. And it works! Sometimes. Here is the code:

#### Loads libraries ######
library('twitteR')
library('RCurl')
library('ROAuth')
library('base64enc')
library('httr')
library('RJSONIO')

# Declare Twitter API Credentials
api_key <- X # From dev.twitter.com
api_secret <- X # From dev.twitter.com
token <- X # From dev.twitter.com
token_secret <- X # From dev.twitter.

# Create Twitter Connection
setup_twitter_oauth(api_key, api_secret, token, token_secret)

# Respond to incoming messages
oldMessage <- dmGet(n = 1)
oldMessage <- oldMessage[[1]]$text

for(i in seq(from = 1, to = 100, by = 1)){

  incomingMessage <- dmGet(n = 1)
  incomingMessage <- incomingMessage[[1]]$text

  if(oldMessage == incomingMessage){

  } else if(incomingMessage == 'Message 1'){
    dmSend('Response 1', 'USER123')
  } else if (incomingMessage == 'Message 2'){
    dmSend('Response 2', 'USER123')
  } else if (incomingMessage == 'Sleep') {
    dmSend('Off', 'USER123')
    break
  }

  oldMessage <- incomingMessage

  Sys.sleep(30)

}

So, effectively, I'm attempting to have it check for new direct messages every 30 seconds, and if the direct message has changed to either 'Message 1', 'Message 2', or 'Sleep', will send a direct message in response, and if the message is 'Sleep', will break out of the loop and stop monitoring for direct messages. The keys and tokens portion is obviously replaced with my own (and my twitter account is not USER123).

The problem is, usually after about 1 or 2 exchanges with R, I receive the following message:

 Error in twInterfaceObj$doAPICall("direct_messages/new", params = params,  : 
  client error: (403) Forbidden 

It is inconsistent in how long it takes. Sometimes it takes 1 message, and I have made it through the full three message sequence once (I've tried about 10 times between yesterday and today). It seems that if I re-run the setup_twitter_oauth statement again, I can start to re-run the script again (still stopping after 1-3 messages), so I'm wondering if I set up something wrong there? For more information, here is my sessionInfo:

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: i686-pc-linux-gnu (32-bit)
Running under: Ubuntu 15.04

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RJSONIO_1.3-0   httr_1.0.0      base64enc_0.1-3 ROAuth_0.9.6    RCurl_1.95-4.7  bitops_1.0-6   
[7] twitteR_1.1.9  

loaded via a namespace (and not attached):
 [1] codetools_0.2-11 digest_0.6.8     R6_2.1.1         DBI_0.3.1        magrittr_1.5     stringi_0.5-5   
 [7] curl_0.9.3       rjson_0.2.15     tools_3.2.2      stringr_1.0.0    bit64_0.9-5      bit_1.1-12

Any ideas why I would be getting this error? I should also mention all other features of the twitteR package that I've tried have worked flawlessly - I can send tweets, search tweets, get user timelines, etc. Thanks for the help!

Edit 9/1/15:

Not sure if it's relevant or helpful, but I tried the same code in Windows 8 (I'm not even close to an expert in Ubuntu, so I wondered if it could make a difference), and received the same error after a message or two.

Edit 9/2/15:

Seems that the error isn't in dmSend, it's in dmGet. If I change the for loop to the following, which just sends the date and time to me every thirty seconds, it happily sends me a flood of messages.

for(i in seq(from = 1, to = 100, by = 1)){
  message <- paste('The current date and time is', Sys.time())
  dmSend(message, 'USER123')
  Sys.sleep(30)
}
1
  • The scroll bar for the code cut off right at might Sys.sleep(30) statement, so it is waiting 30 seconds between each request.
    – Sam
    Sep 1, 2015 at 1:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.