[add] 初始資料

This commit is contained in:
建喵 2021-03-21 22:32:26 +08:00
parent 92dba77dc0
commit ea46097ae9
4 changed files with 224 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
node_modules
.env
package-lock.json
*.pem
.foreverignore
.vscode

132
MessageClass.js Normal file
View File

@ -0,0 +1,132 @@
/** Message */
class MessageClass {
constructor(bot, JianMiaubot) {
this.bot = bot;
this.JianMiaubot = JianMiaubot;
}
Message(event) {
switch (event.message.type) {
case 'text': {
this.Text(event);
break;
}
case 'sticker': {
this.Sticker(event);
break;
}
default:
break;
}
}
Text(event) {
switch (event.source.type) {
case "user": {
this.User(event);
break;
}
case "group":
break;
default:
break;
}
}
async Sticker(event) {
switch (event.source.type) {
case "user": {
let userId = event.source.userId;
let displayName = "";
let profile = await this.bot.getUserProfile(userId);
if (profile) {
displayName = profile.displayName;
}
let replyMsg = `line://app/1602687308-GXq4Vvk9?type=sticker&stk=noanim&sid=${event.message.stickerId}&pkg=${event.message.packageId}`;
//ToJianMiau------------------------------------------------------------------------------------------------------
if (userId !== process.env.toZhuHantoJianMiau) {
let ToJM_message = "已接收訊息:";
ToJM_message += "\ndisplayName: $displayName";
ToJM_message += "\nuserId: $userId";
ToJM_message += "\n" + replyMsg;
let res_toJianMiau = this.JianMiaubot.push(process.env.toJianMiau, ToJM_message);
}
let res_reply = event.reply(replyMsg).then(function (data) {
// 當訊息成功回傳後的處理
}).catch(function (error) {
// 當訊息回傳失敗後的處理
});
break;
}
case "group":
break;
default:
break;
}
}
async User(event) {
let userId = event.source.userId;
let replyMsg = event.message.text;
let displayName = "";
let profile = await this.bot.getUserProfile(userId);
if (profile) {
displayName = profile.displayName;
}
// JianMiau特別功能
if (userId === process.env.toJianMiau || userId === process.env.toZhuHan) {
/** 訊息 */
let Msg = event.message.text.split(" ");
/** 指令 */
let Instruction = Msg[0];
switch (Instruction) {
case "msg":
case "Msg":
case "MSG": {
if (userId == process.env.toZhuHantoJianMiau) {
if (Msg[1] === "豬涵") {
Msg[1] = process.env.toZhuHantoZhuHan;
} else if (Msg[1] === "建喵") {
Msg[1] = process.env.toZhuHantoJianMiau;
}
replyMsg = "";
for (let i = 2; i < Msg.length; i++) {
replyMsg += Msg[i] + (i === Msg.length - 1 ? "" : " ");
}
let res_Msg = this.bot.push(Msg[1], replyMsg);
let ToJM_message = "已發送訊息:";
ToJM_message += `\nuserId: ${Msg[1]}`;
ToJM_message += `\nmessage: ${replyMsg}`;
let res_reply = event.reply(ToJM_message).then(function (data) {
// 當訊息成功回傳後的處理
}).catch(function (error) {
// 當訊息回傳失敗後的處理
});
}
break;
}
default: {
// 使用event.reply(要回傳的訊息)方法可將訊息回傳給使用者
event.reply(replyMsg).then(function (data) {
// 當訊息成功回傳後的處理
}).catch(function (error) {
// 當訊息回傳失敗後的處理
});
break;
}
}
}
}
}
module.exports = MessageClass

66
app.js Normal file
View File

@ -0,0 +1,66 @@
// 背景執行 forever start -w app.js
// 監聽檔案變化 nodemon "npm start"
// Debug node --inspect=192.168.168.15:9229 app.js
require('dotenv').config()
// 引用linebot SDK
var linebot = require('linebot');
const fs = require('fs');
const express = require('express');
const app = express();
//讀取憑證及金鑰
const prikey = fs.readFileSync('privkey.pem', 'utf8');
const cert = fs.readFileSync('cert.pem', 'utf8');
const cafile = fs.readFileSync('chain.pem', 'utf-8');
//建立憑證及金鑰
const credentials = {
key: prikey,
cert: cert,
ca: cafile
};
// 用於辨識Line Channel的資訊
var bot = linebot({
channelId: process.env.toZhuHantoJianMiau,
channelSecret: process.env.ZhuHanchannelSecret,
channelAccessToken: process.env.ZhuHanchannelAccessToken
});
var JianMiaubot = linebot({
channelId: process.env.toJianMiau,
channelSecret: process.env.channelSecret,
channelAccessToken: process.env.channelAccessToken
});
const MessageClass = require('./MessageClass')
const Message = new MessageClass(bot, JianMiaubot);
// 當有人傳送訊息給Bot時
bot.on('event', function (event) {
switch (event.type) {
case 'message': {
Message.Message(event);
break;
}
case 'join':
case 'leave':
case 'follow':
case 'unfollow':
case 'memberJoin':
case 'memberLeave':
case 'postback':
case 'accountLink':
case 'fallback':
default:
break;
}
});
// Bot所監聽的webhook路徑與port
const port = process.env.PORT || 3000;
bot.listen('/linewebhook', port, credentials, function () {
console.log(`listening on ${port}`);
console.log('[BOT已準備就緒]');
});

20
package.json Normal file
View File

@ -0,0 +1,20 @@
{
"name": "line-bot-js",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"linebot": "^1.6.1"
},
"devDependencies": {
"nodemon": "^2.0.7"
},
"scripts": {
"start": "node --inspect=192.168.168.15:9229 app.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}