miladrahimi/twitter-bot

Twitter bot API 的轻量级和简洁实现

v1.3.5 2022-10-17 07:17 UTC

This package is auto-updated.

Last update: 2024-09-17 11:27:24 UTC


README

Latest Stable Version Total Downloads License

Twitter Bot

这个包是一个基本的Twitter机器人,它处理身份验证、HTTP请求和响应以及其他基本需求。它不关注任何特定的Twitter API。相反,它提供了一种工具,使调用Twitter API变得更加简单。

安装

安装Composer并在项目根目录下运行以下命令

composer require miladrahimi/twitter-bot "1.*"

文档

入门

以下代码示例演示了如何创建Twitter Bot的实例。它需要您的消费者密钥(API密钥)和消费者密钥(API密钥秘密)。您可以从Twitter开发者门户网站获取它们。

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
// Your bot is ready!

公开端点

公开端点可以在不进行身份验证和用户令牌的情况下工作。您可以像以下示例那样调用公开端点。此示例显示如何调用搜索API。它搜索包含给定标签的推文。

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
$response = $bot->api('GET', 'search/tweets.json', ['q' => '#pink_floyd']);

if ($response->status() == 200) {
    print_r($response->content());
} else {
    echo $response; // JSON
}

身份验证端点

仅通过身份验证的用户令牌才能使用身份验证端点。用户令牌包括用户令牌和用户令牌秘密。

如果您想代表自己调用Twitter API,您可以从Twitter开发者门户网站获取您的令牌和令牌秘密。如果您需要代表其他用户调用Twitter API,您必须像本文档后面提到的那样在之前授权他们。

此示例显示如何调用更新状态API并发布给定的文本。

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
$bot->setOAuthToken(TOKEN, TOKEN_SECRET);
$response = $bot->api('POST', 'statuses/update.json', ['status' => 'Hello from bot!']);

print_r($response->content());

JSON API

以下示例演示了如何使用JSON正文调用API(如直接消息API)。此示例显示如何调用发送直接消息API并向指定用户发送给定的消息。

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
$bot->setOAuthToken(TOKEN, TOKEN_SECRET);
$response = $bot->apiJson('POST', 'direct_messages/events/new.json', [
    'event' => [
        'type' => 'message_create',
        'message_create' => [
            'target' => [
                'recipient_id' => 666,
            ],
            'message_data' => [
                'text' => 'Hello from bot!',
            ]
        ]
    ]
]);

print_r($response->content());

上传媒体

您可能需要上传媒体。例如,如果您想发推文图片,您首先需要上传它。以下示例说明了如何上传照片并发送它。

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);
$bot->setOAuthToken(TOKEN, TOKEN_SECRET);

$file = __DIR__ . '/files/pink-floyd.jpg';
$mediaResponse = $bot->upload($file, 'POST', 'media/upload.json', [
    'media_category' => 'tweet_image',
    'media_type' => 'image/jpeg',
]);

$response = $bot->api('POST', 'statuses/update.json', [
    'status' => 'Hello from bot!',
    'media_ids' => $mediaResponse->content()['media_id'],
]);

print_r($response->content());

授权用户(通过Twitter登录)

本节解释了如何授权用户以获取他们的令牌和令牌秘密。这实际上是“通过Twitter登录”的实现。

请求令牌和重定向链接

首先,您必须请求一个用户令牌并隐式确认您的回调URL(考虑在您的Twitter开发者门户网站中设置您的回调URL)。然后,您可以重定向用户到Twitter网站以批准您的请求(令牌)。

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);

$response = $bot->oauth('POST', 'oauth/request_token', [
    'oauth_callback' => 'https://your-app.com/twitter/callback',
]);

if ($response->status() == 200) {
    $token = $response->content()['oauth_token'];
    $tokenSecret = $response->content()['oauth_token_secret'];
    
    // Generate twitter redirection url
    $url = $bot->oauthUrl($token);

    // Redirect user to $url
}

Twitter回调

当用户在Twitter网站上批准您的请求(令牌)时,Twitter会将其重定向到您的回调URL。在回调URL中,您可以验证令牌。然后,您可以使用令牌代表用户调用Twitter API。

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);

$response = $bot->oauth('POST', 'oauth/access_token', [
    'oauth_token' => $_REQUEST['oauth_token'],
    'oauth_verifier' => $_REQUEST['oauth_verifier'],
]);

print_r($response->content()); // oauth_token, oauth_token_secret, screen_name, ...

超时

默认情况下,HTTP(cURL)超时为10秒。您可以根据以下示例设置您想要的超时(以秒为单位)。

use MiladRahimi\TwitterBot\V1\TwitterBot;

$bot = TwitterBot::create(CONSUMER_KEY, CONSUMER_SECRET);

$bot->getClient()->setTimeout(13);

许可证

PhpRouter最初由Milad Rahimi创建,并按照MIT许可证发布。