67 lines
1.2 KiB
JavaScript
67 lines
1.2 KiB
JavaScript
const axios = require('axios')
|
|
const config = require('../../config')
|
|
|
|
const client = axios.create({
|
|
baseURL: 'https://api.line.me/v2/bot',
|
|
headers: {
|
|
Authorization: `Bearer ${config.line.access}`
|
|
}
|
|
})
|
|
|
|
/**
|
|
* push message to group or room or user
|
|
* @param {string} target target id (groupid, userid, roomid)
|
|
* @param {string} message push message
|
|
*/
|
|
const pushMessage = async (target, message = '') => {
|
|
if (typeof target !== 'string' || target.trim().length === 0) return
|
|
if (typeof message !== 'string' || message.trim().length === 0) return
|
|
|
|
let data = {
|
|
to: target,
|
|
messages: [
|
|
{
|
|
type: 'text',
|
|
text: message
|
|
}
|
|
]
|
|
}
|
|
|
|
let opts = {
|
|
method: 'post',
|
|
url: '/message/push',
|
|
data
|
|
}
|
|
|
|
await client(opts)
|
|
}
|
|
|
|
/**
|
|
* send reply message
|
|
* @param {string} replyToken line message reply token
|
|
* @param {string} message reply message
|
|
*/
|
|
const replyMessage = async (replyToken, message) => {
|
|
let url = '/message/reply'
|
|
let opts = {
|
|
method: 'post',
|
|
url,
|
|
data: {
|
|
replyToken,
|
|
messages: [
|
|
{
|
|
type: 'text',
|
|
text: message
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
await client(opts)
|
|
}
|
|
|
|
module.exports = {
|
|
pushMessage,
|
|
replyMessage
|
|
}
|