【问题标题】:How to get LINE user mid using PHP SDK?如何使用 PHP SDK 获取 LINE 用户 mid?
【发布时间】:2016-09-03 04:53:47
【问题描述】:

我使用 LINE BOT API 试用 SDK PHP (https://github.com/line/line-bot-sdk-php)。

但是,对于这种方法:

$res = $bot->sendText(['TARGET_MID'], 'Message');

如何获取用户的 MID 向他们发送消息?

感谢您的帮助。

【问题讨论】:

  • 请参阅github.com/line/line-bot-sdk-php/blob/master/src/LINEBot/… 并阅读 LINE BOT API。当用户向 bot 输入内容时,bot 会收到消息并知道是谁发送给 bot。
  • @iownthegame 如果您不想设置 line bot 而只想使用发送消息示例,会发生什么?在这种情况下如何获得目标中点?

标签: php line-api


【解决方案1】:

1) 获取交互式用户的mid 的一种非常快速的方法(真的是更有趣的 hack)是按照API instructions 注册一个回调 URL,然后捕获将 POST 数据发送到该 URL,如下所示:

// /callback/index.php
<?php
$postdata = file_get_contents("php://input");
@file_get_contents('https://'.$_SERVER['SERVER_NAME'].'/LINE/' . json_encode($postdata));

接下来,扫描频道控制台中的二维码,将您的试用机器人添加到您的 LINE 帐户。完成后,向它发送一个快速的“你好!”文本。

然后,如果您愿意,您可以将 POST 数据保存到文本文件中,或者您可以检查服务器日志。例如,您可能会看到如下内容:

163.128.118.223 - - [03/Sep/2016:07:25:25 -0700] "POST /line/callback/ HTTP/1.1" 200 - "-" "ChannelEventDispatcher/1.0"
106.152.218.107 - - [03/Sep/2016:07:25:25 -0700] "GET /LINE/{\"result\":[{\"content\":{\"toType\":1,\"createdTime\":1472114754839,\"from\":\"ub7dbd4a12c322f6c0117773d739c55a4\",\"location\":null,\"id\":\"4357194057879\",\"to\":[\"u2b6a4ba287028dee7291122094dac827\"],\"text\":\"Hello!\",\"contentMetadata\":{\"AT_RECV_MODE\":\"2\",\"SKIP_BADGE_COUNT\":\"true\"},\"deliveredTime\":0,\"contentType\":1,\"seq\":null},\"createdTime\":1472912724868,\"eventType\":\"138311609000106301\",\"from\":\"u236d23c2e36bd87217655609a1c31cb8\",\"fromChannel\":1241102815,\"id\":\"WB1519-3102846635\",\"to\":[\"u2b6a4ba287028dee7291122094dac827\"],\"toChannel\":1462261375}]} HTTP/1.1" 404 15 "-" "-"

\"from\":\"ub7dbd4a12c322f6c0117773d739c55a4\" 是相关部分。


2)如果您想开始使用receiving messages,您可以像这样开始作为您的回调脚本。只需向您的 BOT 发送消息“mid”,它就会回复您的 mid

这是我使用signature verification 为您制作的启动回调脚本。

// /callback/index.php
<?php
// Show all errors for testing
error_reporting(E_ALL);

// SDK is installed via composer
require_once __DIR__ . "/includes/vendor/autoload.php";

use LINE\LINEBot;
use LINE\LINEBot\HTTPClient\GuzzleHTTPClient;

// Set these
$config = [
    'channelId' => LINE_CHANNEL_ID,
    'channelSecret' => LINE_CHANNEL_SECRET,
    'channelMid' => LINE_CHANNEL_MID,
];
$sdk = new LINEBot($config, new GuzzleHTTPClient($config));

$postdata = @file_get_contents("php://input");
$messages = $sdk->createReceivesFromJSON($postdata);

// Verify the signature
// REF: http://line.github.io/line-bot-api-doc/en/api/callback/post.html#signature-verification
$sigheader = 'X-LINE-ChannelSignature';
// REF: http://stackoverflow.com/a/541450
$signature = @$_SERVER[ 'HTTP_'.strtoupper(str_replace('-','_',$sigheader)) ];
if($signature && $sdk->validateSignature($postdata, $signature)) {
    // Next, extract the messages
    if(is_array($messages)) {
        foreach ($messages as $message) {
            if ($message instanceof LINEBot\Receive\Message\Text) {
                $text = $message->getText();
                if ($text == "mid") {
                    $fromMid = $message->getFromMid();

                    // Send the mid back to the sender and check if the message was delivered
                    $result = $sdk->sendText([$fromMid], 'mid: ' . $fromMid);
                    if(!$result instanceof LINE\LINEBot\Response\SucceededResponse) {
                        error_log('LINE error: ' . json_encode($result));
                    }
                } else {
                    // Process normally, or do nothing
                }
            } else {
                // Process other types of LINE messages like image, video, sticker, etc.
            }
        }
    } // Else, error
} else {
    error_log('LINE signatures didn\'t match');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多