sferik / twitter-ruby

A Ruby interface to the Twitter API.
http://www.rubydoc.info/gems/twitter
MIT License
4.58k stars 1.31k forks source link

Unable to post a new DM message using Twitter::REST::Request. #934

Closed GauthamSakthiveeran-zz closed 1 year ago

GauthamSakthiveeran-zz commented 6 years ago

Hi,

I am using a hash like this for posting a DM message and using twitter gem version 5.16 . dm_options = {

  "event" => {
    "type" => 'message_create',
    "message_create" => {
      "target" => {
        "recipient_id" => xxx(recipient handle id)
      },
      "message_data" => {
        "text" => 'DM with Attachment',
      }
    }
  }
}

Trying to post a new DM with ,response = Twitter::REST::Request.new(@client, :post, '/1.1/direct_messages/events/new.json', options).perform.

Its throwing error and unable to perform. Is there a way i can post DM with the twitter gem 5.16.

Can someone help me on this

FabienChaynes commented 6 years ago

Hello @GauthamSakthiveeran

To post a new DM through the DM events endpoint with Twitter::REST::Request#new, you'd need to set the second argument to :json_post (cf. Twitter::REST::DirectMessages#create_direct_message_event) which is only supported since this commit if I'm not wrong.

So no, it's not possible to reach this endpoint with this old version.

I guess you could still try to use the create_direct_message method hitting the deprecated endpoint if it was available back then, but the endpoint it uses will stop working soon.

The other solution would be to tweak your Gemfile to upgrade the gem and use one of the latest commits of this repository.

GauthamSakthiveeran-zz commented 6 years ago

thanks !!

GauthamSakthiveeran-zz commented 6 years ago

@FabienChaynes getting this error, [5] pry(#)> @client.update("HEllo") NoMethodError: undefined method headers' for HTTP:Module from /Users/gauthams/.rvm/gems/ruby-2.5.1/bundler/gems/twitter-844818cad07c/lib/twitter/rest/request.rb:36:inperform'

I have initialized client lije this, client = Twitter::REST::Client.new do |config| config.consumer_key = "YOUR_CONSUMER_KEY" config.consumer_secret = "YOUR_CONSUMER_SECRET" config.access_token = "YOUR_ACCESS_TOKEN" config.access_token_secret = "YOUR_ACCESS_SECRET" end

sqlninja commented 6 years ago

To chime in here as well... I periodically get back an empty response from create_direct_message_event, which causes the method to error on Twitter::DirectMessageEvent.new(response[:event]) because :event is not present on a nil object...

The line for response = Twitter::REST::Request.new(self, :json_post, '/1.1/direct_messages/events/new.json', options).perform doesn't throw an error, it just sets response to nil...

sqlninja commented 6 years ago

Digging into this deeper, I have cloned the gem locally and using the local reference in my gemfile. I've added some print statements to see what's going in the perform method, because this seems to be where it's dying.

I send 3 messages through, one will send and the other two will not (I can replicate this over and over with the same 3 recipients) The 2 that aren't going through are returning a 420 response code, which indicates that I'm being rate limited but I can send to the three recipients again and again with one going through and the other two not, which would lead me to believe it's not a rate limit issue since the one recipient goes through every time.

sqlninja commented 6 years ago

And more info... This seems to be mostly related to the oauth token/secret used to send the message. I used another oauth set and the 3 messages send fine. I would have assumed if this were a follower issue it would throw code 150 : You cannot send messages to users who are not following you.

So honestly I have no clue what is happening, but I am forking the gem to add some better error handling on the lack of a response object...

bobber205 commented 6 years ago

@sqlninja Here's a sample of what I'm doing successfully (for tens of thousands of instances)

  payload = { 
              event: {
                  type: 'message_create',
                  message_create: {
                      target: {
                          recipient_id: user_handle_id
                      },
                      message_data: {
                          text: dm_message
                      }
                  }
              }
          }
        #client is standard Twitter::REST::Client object
        #NEW_DM_ENDPOINT = "/1.1/direct_messages/events/new.json"
        request = Twitter::REST::Request.new(client, :json_post, NEW_DM_ENDPOINT, payload) 
        result = request.perform
sqlninja commented 6 years ago

I think my issue is "sorta" resolved, because I think my issue was linked to my token.