Skip to content

Commit

Permalink
feat(telegram): support image builtin type
Browse files Browse the repository at this point in the history
  • Loading branch information
fer2d2 authored and epaminond committed Sep 10, 2018
1 parent 3f1da72 commit b1ecac6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/channels/botpress-channel-telegram/src/actions.js
Expand Up @@ -26,6 +26,27 @@ const createText = ({ chatId, userId }, text, options = {}) => {
}
}

const createPhoto = ({ chatId, userId }, photo, options = {}) => {
validateChatId(chatId)
validateText(photo) // TODO Refactor validators

if (options.caption) {
validateText(options.caption)
}

return {
platform: 'telegram',
type: 'photo',
text: 'Attachment (photo): ' + photo,
raw: {
chatId: chatId,
to: userId,
photo: photo,
options: options
}
}
}

const createEditMessage = (text, options = {}) => {
return {
platform: 'telegram',
Expand All @@ -40,5 +61,6 @@ const createEditMessage = (text, options = {}) => {

module.exports = {
createText,
createPhoto,
createEditMessage
}
13 changes: 13 additions & 0 deletions packages/channels/botpress-channel-telegram/src/outgoing.js
Expand Up @@ -22,6 +22,18 @@ const handleText = (event, next, telegram) => {
return handlePromise(next, telegram.sendText(chatId, text, options))
}

const handlePhoto = (event, next, telegram) => {
if (event.platform !== 'telegram' || event.type !== 'photo') {
return next()
}

const chatId = event.raw.chatId
const photo = event.raw.photo
const options = event.raw.options

return handlePromise(next, telegram.sendPhoto(chatId, photo, options))
}

const handleEditMessageText = (event, next, telegram) => {
if (event.platform !== 'telegram' || event.type !== 'edited_message_text') {
return next()
Expand All @@ -38,6 +50,7 @@ const handleEditMessageText = (event, next, telegram) => {

module.exports = {
text: handleText,
photo: handlePhoto,
edited_message_text: handleEditMessageText,
pending: {}
}
8 changes: 8 additions & 0 deletions packages/channels/botpress-channel-telegram/src/telegram.js
Expand Up @@ -76,6 +76,14 @@ class Telegram {
})
}

sendPhoto(chatId, photo, options) {
this.validateBeforeSending(chatId, options)

return Promise.fromCallback(() => {
this.bot.sendPhoto(chatId, photo, options)
})
}

sendEditMessage(text, options = {}) {
this.validateBeforeSending(options.chat_id, options)
Telegram.validateText(text)
Expand Down
4 changes: 4 additions & 0 deletions packages/channels/botpress-channel-telegram/src/umm.js
Expand Up @@ -54,6 +54,10 @@ function processOutgoing({ event, blocName, instruction }) {
return actions.createText({ chatId: event.chat.id, userId: getUserId(event) }, instruction.text, options)
}

if (!_.isNil(instruction.photo)) {
return actions.createPhoto({ chatId: event.chat.id, userId: getUserId(event) }, instruction.photo, options)
}

if (!_.isNil(instruction.options) && !_.isNil(instruction.options.reply_markup)) {
return actions.createText(
{ chatId: event.chat.id, userId: getUserId(event) },
Expand Down
Expand Up @@ -35,5 +35,13 @@ export default data => [
image_url: data.image
}
]
},
{
on: 'telegram',
photo: url.resolve(data.BOT_URL, data.image),
options: {
caption: data.title,
parse_mode: 'Markdown'
}
}
]

0 comments on commit b1ecac6

Please sign in to comment.