This repository has been archived on 2024-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
line-cost-js/MessageClass.js
2021-12-26 15:15:28 +08:00

302 lines
13 KiB
JavaScript

const dateFormat = require('dateformat');
const { decode } = require('querystring');
const ToolsClass = require('./ToolsClass');
/** Message */
class MessageClass {
constructor(app) {
this.app = app;
this.Tools = new ToolsClass();
// this.app.CPBL.AddTime({
// game_id: 64,
// cpbldata: {
// "http://www.cpbl.com.tw/games/play_by_play.html?": "",
// game_type: "01",
// game_id: "63",
// game_date: "2021-04-20",
// pbyear: "2021",
// },
// LineID: 1
// });
}
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.app.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.app.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 datetime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
let profile = await this.app.bot.getUserProfile(userId);
if (profile) {
displayName = profile.displayName;
}
//ToJianMiau------------------------------------------------------------------------------------------------------
if (userId !== process.env.toZhuHantoJianMiau) {
let ToJM_message = `已接收訊息:`;
ToJM_message += `\ndisplayName: ${displayName}`;
ToJM_message += `\nuserId: ${userId}`;
ToJM_message += `\nmessage: ${replyMsg}`;
let res_toJianMiau = this.app.JianMiaubot.push(process.env.toJianMiau, ToJM_message);
}
// JianMiau特別功能
if (userId === process.env.toZhuHantoJianMiau || userId === process.env.toZhuHantoZhuHan) {
/** 訊息 */
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.app.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) {
// 當訊息回傳失敗後的處理
});
}
return;
}
case "中職": {
// let URL = Msg[1];
// let Isplay_by_play = URL.indexOf("play_by_play");
// let data = decode(URL);
// if (Isplay_by_play === -1 || !data["game_id"]) {
// return;
// }
// this.app.CPBL.AddTime({
// game_id: data["game_id"],
// cpbldata: data,
// LineID: userId
// }, event);
let GameSno = Msg[1];
if (!GameSno) {
return;
}
let Query = `SELECT UserData.LINENotify FROM \`UserData\` WHERE \`userId\` = '${userId}' LIMIT 1;`;
let res_Query = await this.app.Tools_MYSQLDB.Query(Query);
let access_token = res_Query[0]["LINENotify"];
this.app.CPBL.AddTime({
GameSno: GameSno,
LineID: userId,
access_token: access_token
}, event);
// replyMsg = await this.app.CPBL.GetCPBL1(userId, GameSno);
// event.reply(replyMsg)
return;
}
case "今日賽事":
case "我愛建喵今日賽事": {
let Query = `SELECT UserData.LINENotify FROM \`UserData\` WHERE \`userId\` = '${userId}' LIMIT 1;`;
let res_Query = await this.app.Tools_MYSQLDB.Query(Query);
let access_token = res_Query[0]["LINENotify"];
let columns = await this.app.CPBL.GetCPBLList(Msg[1] ? Msg[1] : dateFormat(new Date(), "yyyy-mm-dd"), access_token);
if (columns.length > 0) {
let res_reply = event.replyimagemap("建喵也愛你", columns).then(function (data) {
// 當訊息成功回傳後的處理
}).catch(function (error) {
// 當訊息回傳失敗後的處理
});
} else {
replyMsg = "今天沒有比賽";
event.reply(replyMsg)
.then(function (data) {
// 當訊息成功回傳後的處理
})
.catch(function (error) {
// 當訊息回傳失敗後的處理
});
}
return;
}
case "CPBL綁定": {
let url = `https://notify-bot.line.me/oauth/authorize?response_type=code&scope=notify&response_mode=form_post&client_id=Jj4K1D7XxtmDeXJGF3c0vV&redirect_uri=https://jianmiau.ml:3333/LINENotify&state=${userId}$${displayName}`;
let shorturl = await this.Tools.shorturl(url);
let Data_columns = [
{
"thumbnailImageUrl": "https://www.line-community.me/awards/uimage/5b46aedf851f74a6af8eaab8",
"imageBackgroundColor": "#FFFFFF",
"title": "點擊綁定",
"text": "點擊後將會進行綁定",
"defaultAction": {
"type": "uri",
"label": "點擊綁定",
"uri": shorturl
},
"actions": [
{
"type": "uri",
"label": "點擊綁定",
"uri": shorturl
}
]
}
];
event.replyimagemap("點擊綁定", Data_columns)
.then(function (data) {
// 當訊息成功回傳後的處理
})
.catch(function (error) {
// 當訊息回傳失敗後的處理
});
return;
}
default: {
let Query = `SELECT * FROM \`line-cost-status\` WHERE \`userId\` = '${userId}' LIMIT 1;`;
let res_Query = await this.app.Tools_MYSQLDB.Query(Query);
let Data = res_Query;
let Status = Data[0]['Status'];
switch (Status) {
case "Love_Pig_QA OK": {
let Extra = JSON.parse(Data[0]["Extra"]);
let answer = false;
let keyword = Extra["keyword"].split(",");
for (let i = 0; i < keyword.length; i++) {
if (!keyword[i] || event.message.text.indexOf(keyword[i]) !== -1) {
answer = true;
}
}
if (answer) {
replyMsg = "答對, 答案就是:";
} else {
replyMsg = "答錯, 正確答案是:";
}
replyMsg += "\n" + Extra["answer"];
Query = `UPDATE \`line-cost-status\` SET \`datetime\`='${datetime}', \`Status\`='', \`Extra\`='' WHERE (\`userId\`='${userId}');`;
res_Query = await this.app.Tools_MYSQLDB.Query(Query);
event.reply(replyMsg).then(function (data) {
// 當訊息成功回傳後的處理
}).catch(function (error) {
// 當訊息回傳失敗後的處理
});
return;
}
case "CPBL RUN": {
switch (Instruction) {
case "停止": {
replyMsg = "已停止中職轉播功能";
Query = `UPDATE \`line-cost-status\` SET \`datetime\`='${datetime}', \`Status\`='', \`Extra\`='' WHERE (\`userId\`='${userId}');`;
res_Query = await this.app.Tools_MYSQLDB.Query(Query);
event.reply(replyMsg).then(function (data) {
// 當訊息成功回傳後的處理
}).catch(function (error) {
// 當訊息回傳失敗後的處理
});
break;
}
default:
break;
}
return;
}
default:
break;
}
break;
}
}
}
if (replyMsg !== "") {
replyMsg = event.message.text;
// 使用event.reply(要回傳的訊息)方法可將訊息回傳給使用者
event.reply(replyMsg).then(function (data) {
// 當訊息成功回傳後的處理
}).catch(function (error) {
// 當訊息回傳失敗後的處理
});
}
}
}
module.exports = MessageClass