pythontwitterpython-twitter

python-twitter error with api.PostDirectMessage


I was testing out code for python-twitter, and everything seems okay until I tried to get it to post a direct message to an id.

api.PostDirectMessage(3517181843, "some text test")

Which then returns

    Traceback (most recent call last):
  File "c:/Users/Azizah Blackwood/Documents/GitHub/just-a-chatbot-test/chatbotwithdifferentlib.py", line 11, in <module>
    api.PostDirectMessage(3517181843, "some text test")
  File "C:\Users\Azizah Blackwood\AppData\Local\Programs\Python\Python37-32\lib\site-packages\twitter\api.py", line 3044, in PostDirectMessage
    created_at=data['event']['created_timestamp'],
KeyError: 'event'

Solution

  • I means the data returned to you is an empty dictionary or the dictionary doesn't have that element.

    data = {}
    print(data['event']['created_timestamp'])
    

    you will get KeyError: 'event'

    You can try passing in return_json=True and see the actual response:

    print(api.PostDirectMessage(3517181843, "some text test", return_json=True))