Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross platform issue: replyImage and sendImage #264

Closed
tigercosmos opened this issue May 10, 2018 · 4 comments
Closed

Cross platform issue: replyImage and sendImage #264

tigercosmos opened this issue May 10, 2018 · 4 comments
Labels

Comments

@tigercosmos
Copy link
Contributor

Considering a condition:
A bot for two platform: a free account Line bot and a Messenger bot.
If I want to reply an image for a message. For Line, I would call replyImage(because I use free account), but there's no replyImage for Messenger, so I would need to call sendImage. But I cannot call sendImage directly, because I use free account for Line.

A simple way to solve this, I write a tricky code for this:

async function platformReplyImage(context, url) {
    if (config.chatroomPlatform == 'messenger') {
        await context.sendImage(url)
    } else {
        await context.replyImage(url);
    }
}

I think it can be a feature for later version to solve this kind problem.(so as sendText)

@tigercosmos tigercosmos changed the title replyImage and sendImage Cross platform issue: replyImage and sendImage May 10, 2018
@tw0517tw
Copy link
Collaborator

tw0517tw commented May 10, 2018

Hi @tigercosmos ,

For the stable release (bottender@0.14.34 or bottender@latest), we have put platform string into context so you can determine there. Maybe it's a better way than use config to check platform.

if (context.platform === 'messenger') {
  // messenger code
}

In the beta version (bottender@0.15.0-beta.9 or bottender@next), we add some new features to LINEBot including setting default send method so you can choose whether to use Reply or Push APIs.

const bot = new LineBot({
  sendMethod: 'reply', // or 'push'
  shouldBatch: true,
  ...otherConfig
});

Then you can call send methods like sendText() or sendImage() and bottender will automatically use Reply API.

Furthermore, with the shouldBatch option sets to true, bottender will batch all the messages and send them per 5 messages so you don't have to worry about the one-time reply token. (If using Reply API, bottender will slice the messages to the length of 5 and ignore the rest.)

// or you can use sendText() if you set sendMethod to 'reply'
await context.replyText('aaa');
await context.replyText('bbb');

// will call Reply API only once with array of 2 messages

The beta features are not documented yet, but you can see the PRs here:

@tigercosmos
Copy link
Contributor Author

I have another question about how to new a bot if I want to cross platform.
as you say, context can know which platform from context.platform.
However, before it we need to either new MessengerBot or new LineBot.
Currently I just use:

const bot = (process.env.chatbotPlatform == 'messenger') ? new MessengerBot() : new LineBot();

I am worried about there is a smarter way.

@tw0517tw
Copy link
Collaborator

tw0517tw commented May 11, 2018

Hi @tigercosmos ,

It's OK to construct different type of Bot with environment variables. In fact, we are currently using something like

const bot =
  process.env.USE_CONSOLE_BOT === 'true'
    ? new ConsoleBot({
        fallbackMethods: true,
      })
    : new MessengerBot({ /* messengerConfig */ });

when developing our own projects.

@chentsulin
Copy link
Collaborator

This question has been well-answered, so I am going to close this issue.

By the way, bottender-compose also has an util function to help this:
https://github.com/Yoctol/bottender-compose#platform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants