mtfosbot/libs/api-action/line.js

132 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-06-28 09:37:33 +00:00
const axios = require('axios')
2018-07-13 05:30:54 +00:00
const config = require('@config/index')
2018-06-28 09:37:33 +00:00
const client = axios.create({
baseURL: 'https://api.line.me/v2/bot',
headers: {
Authorization: `Bearer ${config.line.access}`
}
})
2018-06-28 13:26:44 +00:00
/**
* push message to group or room or user
* @param {string} target target id (groupid, userid, roomid)
* @param {string} message push message
*/
2018-06-28 09:37:33 +00:00
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)
}
2018-07-12 16:29:41 +00:00
const textObject = (txt = '') => {
if (typeof txt !== 'string' || txt.trim().length === 0) return null
return {
type: 'text',
text: txt
}
}
2018-07-13 05:30:54 +00:00
const imageObject = (txt = '') => {
if (typeof txt !== 'string' || txt.trim().length === 0) return null
txt = txt.split(';')
return {
type: 'image',
originalContentUrl: txt[0],
previewImageUrl: txt[1]
}
}
2018-06-28 13:26:44 +00:00
/**
* send reply message
* @param {string} replyToken line message reply token
* @param {string} message reply message
*/
2018-06-28 09:37:33 +00:00
const replyMessage = async (replyToken, message) => {
let url = '/message/reply'
2018-07-12 16:29:41 +00:00
let regex = /^\$(.+?)\$/
let m = message.match(regex)
let obj = null
message = message.replace(/^\$.+?\$/, '')
if (m !== null && m.length > 1) {
switch (m[1]) {
case 'image':
2018-07-13 05:30:54 +00:00
obj = imageObject(message)
2018-07-12 16:29:41 +00:00
break
case 'text':
obj = textObject(message)
break
default:
obj = textObject(message)
2018-06-28 09:37:33 +00:00
}
2018-07-13 08:07:49 +00:00
} else {
obj = textObject(message)
2018-06-28 09:37:33 +00:00
}
2018-07-12 16:29:41 +00:00
if (obj !== null) {
let opts = {
method: 'post',
url,
data: {
replyToken,
messages: [obj]
}
}
await client(opts)
}
2018-06-28 09:37:33 +00:00
}
2018-08-28 15:34:08 +00:00
/**
* query user info
* @param {string} group group id
* @param {string} id user id
*/
const getUserInfo = async (group = '', id = '') => {
if (typeof group !== 'string' || group.trim().length === 0) return null
if (typeof id !== 'string' || id.trim().length === 0) return null
let url = `/group/${group.trim()}/member/${id.trim()}`
try {
let result = await client({
url,
method: 'get'
})
if ('data' in result && typeof result.data === 'object') {
let {displayName, userId} = result.data
if (typeof displayName !== 'string' || displayName.length === 0) return null
if (typeof userId !== 'string' || userId.length === 0) return null
return {
displayName,
userId
}
}
} catch (err) {
console.log('get user info error :::: ', err)
}
return null
}
2018-06-28 09:37:33 +00:00
module.exports = {
pushMessage,
2018-08-28 15:34:08 +00:00
replyMessage,
getUserInfo
2018-06-28 09:37:33 +00:00
}