Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用TwitterAPI使用python发送直接消息_Python_Python 3.x_Twitter_Bots_Twitter Oauth - Fatal编程技术网

使用TwitterAPI使用python发送直接消息

使用TwitterAPI使用python发送直接消息,python,python-3.x,twitter,bots,twitter-oauth,Python,Python 3.x,Twitter,Bots,Twitter Oauth,我正在开发一个python脚本,使用python向twitter上的任何人发送直接消息。为此,我正在使用twitter api,但我不知道如何使用。如果有人知道任何方法,请帮助我。请参阅 from TwitterAPI import TwitterAPI import json api = TwitterAPI(<consumer key>, <consumer secret>, <access

我正在开发一个python脚本,使用python向twitter上的任何人发送直接消息。为此,我正在使用twitter api,但我不知道如何使用。如果有人知道任何方法,请帮助我。

请参阅

from TwitterAPI import TwitterAPI
import json

api = TwitterAPI(<consumer key>, 
                 <consumer secret>,
                 <access token key>,
                 <access token secret>)

user_id = <user id of the recipient>
message_text = <the DM text>

event = {
    "event": {
        "type": "message_create",
        "message_create": {
            "target": {
                "recipient_id": user_id
            },
            "message_data": {
                "text": message_text
            }
        }
    }
}

r = api.request('direct_messages/events/new', json.dumps(event))
print('SUCCESS' if r.status_code == 200 else 'PROBLEM: ' + r.text)