Skip to content

Commit 8c66e45

Browse files
authored
Merge pull request #65 from Dialogue-Bot/DIAL-43-Implement-template-for-line
DIAL-43-Implement-template-for-line
2 parents 7f07e4f + e5c72a3 commit 8c66e45

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

server/src/channels/line.channel.ts

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ export class LineChannel extends BaseChannel {
9494
} catch (e) { }
9595
}
9696

97-
public async sendMessageToUser({ userId, text }) {
98-
const lineUserId = await this.getLineUserID()
97+
public async sendMessageToUser({ userId, text, channelData }) {
98+
const lineUserId = await this.getLineUserID();
9999
try {
100+
if (channelData && channelData.extendData.length) {
101+
return this.detectTemple({ userId, type: channelData.type, extendData: channelData.extendData, text });
102+
}
100103
if (!text) return
101104

102105
await axios({
@@ -120,4 +123,78 @@ export class LineChannel extends BaseChannel {
120123
)
121124
}
122125
}
126+
127+
async detectTemple({ userId, type, extendData, text }) {
128+
switch (type) {
129+
case 'list-card':
130+
return await this.sendGenericTemplate({ userId, extendData });
131+
break;
132+
case 'list-button':
133+
return await this.sendButtons({ userId, buttons: extendData, text });
134+
default:
135+
logger.info(
136+
`[LIN] Line does not support template type ${type}`
137+
);
138+
break;
139+
}
140+
}
141+
142+
async sendButtons({ userId, buttons, text }) {
143+
try {
144+
const option = {
145+
method: 'POST',
146+
url: this.linePostURL + '/message/push',
147+
data: {
148+
to: userId,
149+
messages: [
150+
{
151+
type: "template",
152+
altText: "buttons template",
153+
template: {
154+
type: "confirm",
155+
text: text,
156+
actions: buttons,
157+
}
158+
}
159+
],
160+
},
161+
headers: {
162+
Authorization: 'Bearer ' + this.pageToken,
163+
},
164+
};
165+
166+
await axios(option);
167+
} catch (e) {
168+
console.log('[LIN] send message to User failed: ' + e.message);
169+
}
170+
}
171+
172+
async sendGenericTemplate({ userId, extendData }) {
173+
try {
174+
const option = {
175+
method: 'POST',
176+
url: this.linePostURL + '/message/push',
177+
data: {
178+
to: userId,
179+
messages: [
180+
{
181+
type: "template",
182+
altText: "generic template",
183+
template: {
184+
type: "carousel",
185+
columns: extendData,
186+
}
187+
}
188+
],
189+
},
190+
headers: {
191+
Authorization: 'Bearer ' + this.pageToken,
192+
},
193+
};
194+
195+
await axios(option);
196+
} catch (e) {
197+
console.log('LNE send message to User failed');
198+
}
199+
}
123200
}

server/src/services/conversation.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class ConversationService {
5858
return await lineChannel.sendMessageToUser({
5959
userId: recipient.id,
6060
text,
61+
channelData,
6162
})
6263

6364
break

0 commit comments

Comments
 (0)