Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct Message API changes #1081

Closed
j-e-d opened this issue Jul 20, 2018 · 24 comments · Fixed by #1228
Closed

Direct Message API changes #1081

j-e-d opened this issue Jul 20, 2018 · 24 comments · Fixed by #1228
Assignees
Labels
API This is regarding Twitter's API Bug This is regarding a bug with the library Improvement This is regarding an improvement to an existing feature
Milestone

Comments

@j-e-d
Copy link

j-e-d commented Jul 20, 2018

Twitter is deprecating some of it's REST API endpoints for Direct Messages on August 16, 2018 and replacing them with new endpoints that require changes in how data is sent, they will also return DMs with a different format. More details in the Direct Message Migration guide and in the announcement.

Edit: new reminder from twitter: https://twittercommunity.com/t/reminder-site-streams-user-streams-and-legacy-dm-endpoints-will-be-sunset-august-16th/109482

@do-n-khanh
Copy link

twitter is going to shut down Direct Message API today. Is there any update? Thanks.

@j-e-d
Copy link
Author

j-e-d commented Aug 16, 2018

Update from Twiiter:

The legacy Direct Message endpoints will flicker periodically for one month. At the end of that period, on September 17, requests to them will return errors. During the flicker period, we will also return 404 error messages. We will share details about the flickering schedule for these DM endpoints here on the forums soon.

https://twittercommunity.com/t/details-and-what-to-expect-from-the-api-deprecations-this-week-on-august-16-2018/110746

@do-n-khanh
Copy link

do-n-khanh commented Aug 17, 2018

Because I just need to send direct message, where is a quick fix for anyone who wants it right now:
do-n-khanh@79772c9#diff-ea5dd38a4efd9ff36c96e04ab0597cfb

If you are on version 2.3.0 (like me), that is all you need. But if you are on version 3.6.0, then you need add 3 lines of code to binder.py (quick and dirty fix)
do-n-khanh@c978749

You can send DM like:

event = {
  "event": {
    "type": "message_create",
    "message_create": {
      "target": {
        "recipient_id": 'user_id'
      },
      "message_data": {
        "text": 'This is a test'
      }
    }
  }
}
api.send_direct_message_new(event)

@StefTocup
Copy link

StefTocup commented Aug 17, 2018

@do-n-khanh When I'm using your api.py over a 3.6.0 it doesn't work

Traceback (most recent call last):
  File "Test.py", line 48, in <module>
    a = Test()
  File "Test.py", line 28, in __init__
    self.send_direct_message(self.destname,"test")
  File "Test.py", line 44, in send_direct_message
    self.api.send_direct_message_new(event)        
  File "/usr/local/lib/python2.7/dist-packages/tweepy/api.py", line 688, in send_direct_message_new
    )(self, post_data=post_data, headers=headers)
  File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 246, in _call
    method = APIMethod(args, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 62, in __init__
    self.build_parameters(args, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 99, in build_parameters
    raise TweepError('Too many parameters supplied!')
tweepy.error.TweepError: Too many parameters supplied!
        try :
            self.config = ConfigParser.ConfigParser()
            self.config.read(os.path.expanduser('~/Prog/config/.twitterrc'))
            consumer_key=self.config.get("security","consumer_key")
            access_token=self.config.get("security",  "access_token")
            consumer_secret=self.config.get("security","consumer_secret")
            access_token_secret=self.config.get("security","access_token_secret")
            auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
            auth.set_access_token(access_token, access_token_secret)
        except ConfigParser.NoSectionError:
            print("Section security non trouvée")
        self.configT = ConfigParser.ConfigParser()
        self.configT.read(os.path.expanduser('~/Prog/config/.TwittingDoor'))
        try :
            self.destname=self.configT.get("twitter","destname")
        except ConfigParser.NoSectionError:
            print("Section twitter non trouvée")

        self.api = tweepy.API(auth)
        self.send_direct_message(self.destname,"test")

    def send_direct_message(self,dest, msg):
        event = {
            "event": {
                "type": "message_create",
                "message_create": {
                    "target": {
                        "recipient_id": dest
                    },
                    "message_data": {
                        "text": msg
                    }
                }
            }
        }
        self.api.send_direct_message_new(event)

@do-n-khanh
Copy link

@StefTocup: Sorry, my version is 2.3.0. Let's me see how it works in 3.6.0

@do-n-khanh
Copy link

@StefTocup: I made a dirty fix to make it work in 3.6.0 by adding 3 lines of code to binder.py

do-n-khanh@c978749

Let's me know if it works for you.

@StefTocup
Copy link

@do-n-khanh it's working with this quick fix.
But using screen name no longer work, I had to search for id using :

user = self.api.get_user(self.destname)
self.dest_id = user.id

@do-n-khanh
Copy link

yes, it is because twitter api only allow user id.

@ZeroCoolHacker
Copy link

It is not working.

@arturoabruzzini
Copy link

Hi, has anyone picked this up? Do we have an idea of when Tweepy will be migrated to the new API?

@do-n-khanh
Copy link

It is not working.

Please paste the error log.

@Exceen
Copy link

Exceen commented Sep 21, 2018

I'm using tweepy 3.6.0 and it doesn't work for me either. I added the lines from your commit do-n-khanh@c978749 and noticed that the path seems to be /direct_messages/new.json and not /direct_messages/events/new.json. I corrected this but I still get the same error message.

Printing args gives (u'Exceen_',) and kwargs gives {'text': u'@priyapsingh84 and @mmx05202147 followed you.'} which you're overwriting with an empty set in your fix.

Traceback (most recent call last):
  File "/usr/local/bin/followings", line 80, in <module>
    main()
  File "/usr/local/bin/followings", line 62, in main
    notify_via_dm(followings, api_bot, '%s followed you.', new_followers)
  File "/usr/local/bin/followings", line 73, in notify_via_dm
    response = api.send_direct_message(recipient, text=message)
  File "/home/pi/.local/lib/python2.7/site-packages/tweepy/binder.py", line 257, in _call
    return method.execute()
  File "/home/pi/.local/lib/python2.7/site-packages/tweepy/binder.py", line 241, in execute
    raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{u'message': u'Sorry, that page does not exist.', u'code': 34}]

What I don't understand is how this should fix the problem. You're essentially removing the two important arguments to send a DM. But then again I didn't look into tweepy's code so I have 0 understanding of what tweepy does. I just seems quite weird and looks like you're not just preventing the error message but also the DM from being sent.


Figured out that the path seems to have been changed to this. (https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event)

Now I just need to figure out how to modify the data tweepy sends so that the Twitter API properly understands my request.

@do-n-khanh
Copy link

do-n-khanh commented Sep 21, 2018

Hi Exceen, it works for me (on 2.3.0 and 3.6.0) and @StefTocup (3.6.0)

if you are on version 3.6.0, then you need:

  1. Add 2 methods to api.py file:
    do-n-khanh@79772c9#diff-ea5dd38a4efd9ff36c96e04ab0597cfb

  2. Add 3 lines on binder.py
    do-n-khanh@c978749

To send DM, you need to use

event = {
  "event": {
    "type": "message_create",
    "message_create": {
      "target": {
        "recipient_id": 'user_id'
      },
      "message_data": {
        "text": 'This is a test'
      }
    }
  }
}
api.send_direct_message_new(event)

Note that we are using send_direct_message_new.

@bakayim
Copy link

bakayim commented Sep 24, 2018

I fixed "get" part of DM. Waiting my pull request to be merged

You can find pull request at: #1109
You can use my forked repo: https://github.com/bakayim/tweepy

@FdMon
Copy link

FdMon commented Sep 25, 2018

Hi do-n-khanh

I´m on version 3.6.0. I added the 2 methods and the lines on binder.py

but when I import tweepy this new funtion "send_direct_message_new" is not available. I get the error api object has no attribute "send_direct_message_new"

I started to use tweepy and python a couple o months ago. Maybe i do something wrong.

Thanks in advance for your help

@do-n-khanh
Copy link

what framework or environment are you working on? you need to make sure that the modified tweepy lib is called instead of the old one.

and please make sure 2 methods are put in api.py file.

lastly, you need to paste the error log so that i can figure out the error.

@FdMon
Copy link

FdMon commented Sep 25, 2018

do-n-khanh, your point. I had changed a old lib....

now it works fine!!! thanks a lot!!!

@huangweibuct
Copy link

Hi do-nkhanh,

Have you met this error: "tweepy.error.TweepError: [{u'message': u'You cannot send messages to users who are not following you.', u'code': 150}]"

I wanted to use send_direct_message_new to send message to some users who is not following me and whom I'm not following too.

Any ideas?

Thanks!

@do-n-khanh
Copy link

that is impossible due to twitter rules

@Dixit22
Copy link

Dixit22 commented Dec 31, 2018

I had to apply the above fixes ( #1081 (comment) ) to get version 3.7.0 to work. That was the version pip install tweepy gave me on 28-Dec and I was a bit surprised such a new version didn't already include them.

@Harmon758 Harmon758 added API This is regarding Twitter's API Bug This is regarding a bug with the library Improvement This is regarding an improvement to an existing feature labels Apr 28, 2019
@lshaf
Copy link

lshaf commented Apr 28, 2019

Do I need to fetch it manually from bind_api?
I'm using version 3.7.0 and can't fetch DM due to outdated urls

https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference

@machine-uprising
Copy link

I'm working on a fork of tweepy to update the direct message endpoints as well as adding the new functionality of quick replies, buttons and the async media upload that is now required for direct messages. I'm also working on including the new Welcome Message endpoints and Welcome Message Rules. There's been a fair bit of work involved in this and it's a pretty significant update as it affects multiple files in tweepy (api, binder, cursor, models, parsers), adds a new file (mediaasync.py) that is a modified version of binder.py for the async media uploader. These updates also add to some of the key design methodology on which tweepy operates in api.py and binder.py. The cursor styles for direct messages and welcome messages are also different to the standard cursor method used by the API which adds further complexity to the updates.

You can find the fork I'm working on here https://github.com/machine-uprising/tweepy/tree/direct_messages
The working branch in this repository is the 'direct_messages' branch but both branches (master and direct_messages) are being updated with recent pull requests from the tweepy master branch that @Harmon758 has been updating.

To keep track of the changes made for documentation and for testing I have a wiki setup in this repository - https://github.com/machine-uprising/tweepy-devtest/wiki The wiki has details about the updates being made and examples for testing/using the new endpoints.

This would be a pretty major pull request. Also, because of the changes required the Travis CI tests are failing on the new endpoints of direct messages and will likely require rerecording the cassettes. This would need input from @Harmon758 and/or @joshthecoder

If anyone wants to setup their own virtual environment and run some testing that would be a great help.

@pedrodejesus
Copy link

Hi, I'm using 3.7.0 and DM methods are still not working. There are any ways to get it fixed?

@Harmon758
Copy link
Member

I've now made a branch (https://github.com/tweepy/tweepy/tree/dm-migration) and draft PR (#1228) for the Direct Message migration. It should fully support the new changes.
Feel free to test it and report any issues you encounter.

@huangweibuct This is not something that Twitter allows and is consistent with the website/app.
See https://help.twitter.com/en/using-twitter/direct-messages.

@machine-uprising #1228 simply restores basic DM functionality.
Feel free to PR specific additional features.
Although, your fork seems to be an amalgamation of various changes and I would recommend splitting each component into a separate PR so that it's easier to review and merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API This is regarding Twitter's API Bug This is regarding a bug with the library Improvement This is regarding an improvement to an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.