2020年1月14日 星期二

LINE機器人傳sticker貼圖

這一篇介紹如何讓LINE機器人辦識使用者傳來的sticker,並回傳sticker給使用者。



stickers可以讓對話更有趣、更生動。





---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

以下是PHP程式碼,先將所有會用到的sticker列出來,然後判斷LINE傳來的資料型態是否為sticker,然後再隨機挑一個一開始列出來的sticker,回傳給使用者。



程式碼


$_CONFIG['arrStickerNum']['1'] = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,21,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430);

$_CONFIG['arrStickerNum']['2'] = array(18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527);

$_CONFIG['arrStickerNum']['11537'] = array(52002734,52002735,52002736,52002737,52002738,52002739,52002740,52002741,52002742,52002743,52002744,52002745,52002746,52002747,52002748,52002749,52002750,52002751,52002752,52002753,52002754,52002755,52002756,52002757,52002758,52002759,52002760,52002761,52002762,52002763,52002764,52002765,52002766,52002767,52002768,52002769,52002770,52002771,52002772,52002773);

$_CONFIG['arrStickerNum']['11538'] = array(51626494,51626495,51626496,51626497,51626498,51626499,51626500,51626501,51626502,51626503,51626504,51626505,51626506,51626507,51626508,51626509,51626510,51626511,51626512,51626513,51626514,51626515,51626516,51626517,51626518,51626519,51626520,51626521,51626522,51626523,51626524,51626525,51626526,51626527,51626528,51626529,51626530,51626531,51626532,51626533);



$_CONFIG['token'] = '';



include ('lineJSONmaker.class.php');

class LineJSONmaker3 extends LineJSONmaker{

public function setSticker($packageId, $stickerId){


$json = <<
{

"type": "sticker",

"packageId": "$packageId",

"stickerId": "$stickerId"

}

EOT;

$this->arrJSON[] = $json;

}



}



$raw = file_get_contents('php://input');

$data = json_decode($raw, true);



$objLineJSON = new LineJSONmaker3($_CONFIG['token'], $data['events'][0]['replyToken']);

if($data['events'][0]['message']['type'] == 'sticker')

{

$keysPackage = array_keys($_CONFIG['arrStickerNum']);

$indexPackage = rand(0,count($keysPackage)-1);

$packageId = $keysPackage[$indexPackage];

$indexSticker = rand(0,count($_CONFIG['arrStickerNum'][$packageId])-1);

$stickerId = $_CONFIG['arrStickerNum'][$packageId][$indexSticker];

$objLineJSON->setSticker($packageId,$stickerId);

}

$result = $objLineJSON->send();





public function setSticker($packageId, $stickerId){

$json = <<
{

"type": "sticker",

"packageId": "$packageId",

"stickerId": "$stickerId"

}

EOT;

$this->arrJSON[] = $json;

}

?>









 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------




下列是LINE官方文件所列出的sticker代號表,下面列出我會用到的部份。

Sticker列表








































沒有留言:

張貼留言

IKEA吊櫃廚櫃

 好不容易裝好IKEA買來的吊櫃,花了三天。 從組裝,鑽牆,上牆調水平,累死我了。