Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict'

/**
* @author Eden Cha <eden@nurigo.net>
*/

const qs = require('qs')
const { asyncRequest } = require('./utils')
const { getAuth } = require('./config')

module.exports = class Storage {
constructor (options = {}) {
const { fileId } = options
this.fileId = fileId
}

getFileId () {
return this.fileId
}

setFileId (fileId) {
this.fileId = fileId
}

/**
* 파일 업로드
*
* @param {object} params - 서버 요청 시 사용되는 파라미터 입니다.
* @example
* // return promise object
* Storage.upload({
* type: '파일 타입'
* }).then(body => {
* console.log(body)
* })
*/
upload (params = {}) {
const { file, name, type, link } = params
return asyncRequest('post', 'https://api.solapi.com/storage/v1/files', {
headers: { Authorization: getAuth() },
form: { file, name, type, link }
})
}

/**
* 업로드 된 파일 조회
*
* @param {object} params - 서버 요청 시 사용되는 파라미터 입니다.
* @example
* // return promise object
* Storage.get({
* fileId: '이미지 아이디 '
* }).then(body => {
* console.log(body)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

띄어쓰기

* })
*/
get (params = {}) {
const queryString = qs.stringify({ fileId: this.fileId, ...params })
const uri = 'https://api.solapi.com/storage/v1/files'
return asyncRequest('get', uri + (queryString ? '?' + queryString : ''), {
headers: { Authorization: getAuth() }
})
}
}
47 changes: 47 additions & 0 deletions example/sendSimpleAta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { config, Group } = require('../')
config.init({
// https://solapi.com/credentials
apiKey: 'ENTER_YOUR_API_KEY',
apiSecret: 'ENTER_YOUR_API_SECRET'
})
const messageParams = {
type: 'ATA',
subject: 'this is ATA',
text: 'Hello, this is ATA',
to: '수신 번호',
// https://solapi.com/senderids
from: '발신 번호',
kakaoOptions: {
disableSms: true,
// https://solapi.com/kakao/plus-friends
pfId: 'ENTER_YOUR_PFID',
// https://solapi.com/kakao/templates
templateId: 'ENTER_YOUR_TEMPLATE_ID',
buttons: [
{
buttonName: 'YOUR_BUTTON_NAME',
buttonType: 'YOUR_BUTTON_TYPE',
linkMo: 'YOUR_BUTTON_LINK',
linkPc: 'YOUR_BUTTON_LINK',
linkAnd: 'YOUR_BUTTON_LINK',
linkIos: 'YOUR_BUTTON_LINK'
}
]
}
}

// send ATA (알림톡)
main()

async function main () {
try {
const response = await send(messageParams)
console.info(response)
} catch (error) {
console.error(error)
}
}

function send (params, agent = {}) {
return Group.sendSimpleMessage(params, agent)
}
92 changes: 92 additions & 0 deletions example/sendSimpleCti.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const path = require('path')
const image2base64 = require('image-to-base64')
const { config, Group, Storage } = require('../')
config.init({
apiKey: 'ENTER_YOUR_API_KEY',
apiSecret: 'ENTER_YOUR_API_SECRET'
})
const options = {
imagePath: path.join(__dirname, '/source/tmp_img.png')
}
const storageParams = {
// https://solapi.com/storage
fileId: 'ENTER_YOUR_FILE_ID',
name: 'ENTER_YOUR_IMAGE_NAME',
link: 'ENTER_YOUR_IMAGE_LINK',
type: 'KAKAO'
}
const messageParams = {
type: 'CTI',
subject: 'this is CTI',
text: 'Hello, this is CTI',
to: '수신 번호',
from: '발신 번호',
kakaoOptions: {
disableSms: true,
// https://solapi.com/kakao/plus-friends
pfId: 'ENTER_YOUR_PFID',
imageId: storageParams.fileId,
buttons: [
{
buttonName: '홈페이지',
buttonType: 'WL',
linkMo: 'https://solapi.com',
linkPc: 'https://solapi.com'
},
{
buttonName: '회원가입',
buttonType: 'WL',
linkMo: 'https://solapi.com/signup',
linkPc: 'https://solapi.com/signup'
}
]
}
}

// send CTI (친구톡 이미지)
const { fileId } = storageParams
const storage = new Storage({ fileId })
main()

async function main () {
try {
const { fileList = [] } = await getImages({ fileId: storage.getFileId() })
const image = fileList[0] || {}
if (!image.fileId) {
const { imagePath } = options
const { type, name, link } = storageParams
const imageOptions = { type, name, link }
const response = await uploadImage(imagePath, imageOptions)
storage.setFileId(response.fileId)
}
messageParams.kakaoOptions.imageId = storage.getFileId()
const response = await send(messageParams)
console.info(response)
} catch (error) {
console.error(error)
}
}

function uploadImage (path, params) {
return new Promise((resolve, reject) => {
image2base64(path).then(file => {
storage
.upload({ file, ...params })
.then(resolve)
.catch(reject)
})
})
}

function getImages (params) {
return new Promise((resolve, reject) => {
storage
.get(params)
.then(resolve)
.catch(reject)
})
}

function send (params, agent = {}) {
return Group.sendSimpleMessage(params, agent)
}
73 changes: 73 additions & 0 deletions example/sendSimpleMms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const path = require('path')
const image2base64 = require('image-to-base64')
const { config, Group, Storage } = require('../')
config.init({
// https://solapi.com/credentials
apiKey: 'ENTER_YOUR_API_KEY',
apiSecret: 'ENTER_YOUR_API_SECRET'
})
const options = {
imagePath: path.join(__dirname, '/source/tmp_img.png')
}
const storageParams = {
// https://solapi.com/storage
fileId: 'ENTER_YOUR_FILE_ID',
type: 'MMS'
}
const messageParams = {
type: 'MMS',
subject: 'this is MMS',
text: 'Hello, this is MMS',
to: '수신 번호',
// https://solapi.com/senderids
from: '발신 번호',
imageId: storageParams.fileId
}

// send MMS (멀티미디어메시지)
const { fileId } = storageParams
const storage = new Storage({ fileId })
main()

async function main () {
try {
const { fileList = [] } = await getImages({ fileId: storage.getFileId() })
const image = fileList[0] || {}
if (!image.fileId) {
const { imagePath } = options
const { type } = storageParams
const imageOptions = { type }
const response = await uploadImage(imagePath, imageOptions)
storage.setFileId(response.fileId)
}
messageParams.imageId = storage.getFileId()
const response = await send(messageParams)
console.info(response)
} catch (error) {
console.error(error)
}
}

function uploadImage (path, params) {
return new Promise((resolve, reject) => {
image2base64(path).then(file => {
storage
.upload({ file, ...params })
.then(resolve)
.catch(reject)
})
})
}

function getImages (params) {
return new Promise((resolve, reject) => {
storage
.get(params)
.then(resolve)
.catch(reject)
})
}

function send (params, agent = {}) {
return Group.sendSimpleMessage(params, agent)
}
Binary file added example/source/tmp_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
const config = require('./config')
const Group = require('./Group')
const Image = require('./Image')
const Storage = require('./Storage')

module.exports = {
config,
group: Group,
Group,
image: Image,
Image
Image,
storage: Storage,
Storage
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"dependencies": {
"crypto-js": "^3.1.9-1",
"fs": "0.0.1-security",
"image-to-base64": "^2.0.1",
"moment": "^2.22.1",
"nanoid": "^2.0.3",
"path": "^0.12.7",
"qs": "^6.5.2",
"qs": "^6.9.1",
"request": "^2.85.0"
},
"devDependencies": {
Expand Down
Binary file added tests/source/tmp_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* @author Henry Kim <henry@nurigo.net>
*/

const path = require('path')
const { expect } = require('chai')
const { group: Group } = require('../')
const { Group, Storage } = require('../')
const { getAuth, getTo, getFrom } = require('../config')
const image2base64 = require('image-to-base64')

describe('test', () => {
describe('config', () => {
Expand Down Expand Up @@ -182,4 +184,14 @@ describe('test', () => {
})).to.have.all.keys('groupId', 'to', 'from', 'type', 'statusMessage', 'messageId', 'statusCode', 'accountId', 'country')
})
})
describe('Storage', () => {
it('파일업로드', async () => {
const storage = new Storage()
const file = await image2base64(path.join(__dirname, '/source/tmp_img.png'))
const uploadResponse = await storage.upload({ file })
expect(uploadResponse).to.have.all.keys('kakao', 'type', 'originalName', 'url', 'fileId', 'accountId', 'dateCreated', 'dateUpdated', 'link', 'name', 'references')
const getResponse = await storage.get({ fileId: uploadResponse.fileId })
expect(getResponse).to.have.all.keys('fileList', 'nextKey', 'startKey')
})
})
})
3 changes: 2 additions & 1 deletion utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const request = require('request')
function asyncRequest (method, uri, data) {
return new Promise((resolve, reject) => {
request[method](uri, data, (err, res) => {
if (err) return reject(err)
res.body = JSON.parse(res.body || '{}')
if (err || res.statusCode !== 200) reject(err || res.body)
if (res.statusCode !== 200) return reject(res.body)
resolve(res.body)
})
})
Expand Down
Loading