我最近让LINE Bot负责托管。 https://github.com/line/line-bot-sdk-php
我可以使用在这里找到的设置https://github.com/afifmakarim/bot-line-simple
这是令人困惑的部分,由于对名称空间的了解不足,我迷失了代码。显然,要使LINE Bot从mysql数据库中检索数据并将其作为消息发送回去,我需要使用名称空间类。
无论如何我迷路了,有人可以指出我正确的方向吗?
编辑: 这是index.php
<?php
require __DIR__ . '/vendor/autoload.php';
include 'include/dbconfig.php';
include 'include/function.php';
use\ LINE\ LINEBot\ SignatureValidator as SignatureValidator;
load config
$dotenv = new Dotenv\ Dotenv( __DIR__ );
$dotenv->load();
initiate app
$configs = [
settings' => [ 'displayErrorDetails' => true ],
];
$app = new Slim\ App( $configs );
ROUTES
$app->get( '/', function ( $request, $response ) {
return "Working!";
} );
$app->post( '/', function ( $request, $response ) {
get request body and line signature header
$body = file_get_contents( 'php://input' );
$signature = $_SERVER[ 'HTTP_X_LINE_SIGNATURE' ];
log body and signature
file_put_contents( 'php://stderr', 'Body: ' . $body );
is LINE_SIGNATURE exists in request header?
if ( empty( $signature ) ) {
return $response->withStatus( 400, 'Signature not set' );
}
is this request comes from LINE?
if ( $_ENV[ 'PASS_SIGNATURE' ] == false && !SignatureValidator::validateSignature( $body, $_ENV[ 'CHANNEL_SECRET' ], $signature ) ) {
return $response->withStatus( 400, 'Invalid signature' );
}
init bot
$httpClient = new\ LINE\ LINEBot\ HTTPClient\ CurlHTTPClient( $_ENV[ 'CHANNEL_ACCESS_TOKEN' ] );
$bot = new\ LINE\ LINEBot( $httpClient, [ 'channelSecret' => $_ENV[ 'CHANNEL_SECRET' ] ] );
$data = json_decode( $body, true );
foreach ( $data[ 'events' ] as $event ) {
$userMessage = $event[ 'message' ][ 'text' ];
$msgchunk = explode( " ", $userMessage );
Hello Example
if ( strtolower( $msgchunk[ 0 ] ) == 'hello' ) {
if ( strtolower( $msgchunk[ 1 ] ) == 'you' ) {
$message = "Hi you";
$textMessageBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TextMessageBuilder( $message );
$result = $bot->replyMessage( $event[ 'replyToken' ], $textMessageBuilder );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
} else {
$message = "Hi";
$textMessageBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TextMessageBuilder( $message );
$result = $bot->replyMessage( $event[ 'replyToken' ], $textMessageBuilder );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
}
}
Search for something
if ( strtolower( $msgchunk[ 0 ] ) == 'search' ) {
if ( strtolower( $msgchunk[ 1 ] ) == 'id' ) {
if ( !$msgchunk[ 2 ] ) {
$message = "Please include an id to search for.";
$textMessageBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TextMessageBuilder( $message );
$result = $bot->replyMessage( $event[ 'replyToken' ], $textMessageBuilder );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
} else {
This is where i want it to retrieve data from mysql database using the getName($userid) function.
$message = getName($db_con,$userid);
$textMessageBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TextMessageBuilder( $message );
$result = $bot->replyMessage( $event[ 'replyToken' ], $textMessageBuilder );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
}
} else {
$message = "What do you want to search for?
Try, Search id or Search Sub";
$textMessageBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TextMessageBuilder( $message );
$result = $bot->replyMessage( $event[ 'replyToken' ], $textMessageBuilder );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
}
}
if ( strtolower( $userMessage ) == 'add member' ) {
$message = "Adding Member";
$textMessageBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TextMessageBuilder( $message );
$result = $bot->replyMessage( $event[ 'replyToken' ], $textMessageBuilder );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
}
if ( strtolower( $userMessage ) == 'find member' ) {
$message = "Finding Member";
$textMessageBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TextMessageBuilder( $message );
$result = $bot->replyMessage( $event[ 'replyToken' ], $textMessageBuilder );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
}
if ( strtolower( $userMessage ) == 'ihelp' ) {
$carouselTemplateBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TemplateBuilder\ CarouselTemplateBuilder( [
new\ LINE\ LINEBot\ MessageBuilder\ TemplateBuilder\ CarouselColumnTemplateBuilder(
"Search",
"use this to search for member id.",
"https://myanimelist.cdn-dena.com/images/characters/8/320273.jpg", [
new\ LINE\ LINEBot\ TemplateActionBuilder\ UriTemplateActionBuilder( 'Search', "Search id 15361839" ),
] ),
new\ LINE\ LINEBot\ MessageBuilder\ TemplateBuilder\ CarouselColumnTemplateBuilder(
"Sub",
"use this to get a list of subs for an id",
"https://myanimelist.cdn-dena.com/images/characters/8/320273.jpg", [
new\ LINE\ LINEBot\ TemplateActionBuilder\ UriTemplateActionBuilder( 'Sub', "Sub id 15361839" ),
] ),
] );
$templateMessage = new\ LINE\ LINEBot\ MessageBuilder\ TemplateMessageBuilder( 'template name', $carouselTemplateBuilder );
$result = $bot->replyMessage( $event[ 'replyToken' ], $templateMessage );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
}
if ( strtolower( $userMessage ) == "help" ) {
$buttonTemplateBuilder = new\ LINE\ LINEBot\ MessageBuilder\ TemplateBuilder\ ButtonTemplateBuilder(
"title",
"text",
"https://i0.wp.com/angryanimebitches.com/wp-content/uploads/2013/03/tamakomarket-overallreview-tamakoanddera.jpg", [
new\ LINE\ LINEBot\ TemplateActionBuilder\ MessageTemplateActionBuilder( 'Action Button', 'action' ),
] );
$templateMessage = new\ LINE\ LINEBot\ MessageBuilder\ TemplateMessageBuilder( 'template name', $buttonTemplateBuilder );
$result = $bot->replyMessage( $event[ 'replyToken' ], $templateMessage );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
}
if ( $userMessage == "Send image" ) { //your message
$imageMessage = new\ LINE\ LINEBot\ MessageBuilder\ ImageMessageBuilder( "https://myanimelist.cdn-dena.com/images/characters/8/320273.jpg", "https://myanimelist.cdn-dena.com/images/characters/8/320273.jpg" );
$result = $bot->replyMessage( $event[ 'replyToken' ], $imageMessage );
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
}
}
} );
$app->get('/push/{to}/{message}', function ($request, $response, $args)
{
$httpClient = new \LINE\LINEBot\HTTPClient\CurlHTTPClient($_ENV['CHANNEL_ACCESS_TOKEN']);
$bot = new \LINE\LINEBot($httpClient, ['channelSecret' => $_ENV['CHANNEL_SECRET']]);
$textMessageBuilder = new \LINE\LINEBot\MessageBuilder\TextMessageBuilder($args['message']);
$result = $bot->pushMessage($args['to'], $textMessageBuilder);
return $result->getHTTPStatus() . ' ' . $result->getRawBody();
});
JUST RUN IT
$app->run();
这是function.php
<?php
function getName( $db_con, $userid ) {
$dstmt = $db_con->prepare( "SELECT * FROM players WHERE p_id=:userid" );
$dstmt->bindParam( ":userid", $userid );
$dstmt->execute();
$drow = $dstmt->fetch();
return $drow[ 'pname' ];
}
?>
这是dbconfig.php
$db_host = "localhost";
$db_name = "*****";
$db_user = "*****";
$db_pass = "*****";
try{
$db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
$dbh = $db_con;
$db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
这是我尝试过的方法,它不起作用。如果有人想指出任何事情,请执行。 我并不是要成为专家编码员,这只是我似乎开发的一种爱好。