snlangsuan / line-messaging

SDK of the LINE Messaging API for Node.js
38 stars 9 forks source link

Cant get reply token #10

Open jpaul55 opened 6 years ago

jpaul55 commented 6 years ago

Hi, Thank you for the module. just want to ask why do I cant get a response for this line of code. even a console .log

bot.on(LINEBot.Events.MESSAGE, function(replyToken, message) { // add code below. console.log('Test'); });

snlangsuan commented 6 years ago

I want to see before this code. thank you

jpaul55 commented 6 years ago

Thank you for the reply, this is the code. Am I missing something? my webhook url. is like this. https://line-webhook-demo.herokuapp.com

var app = require('express')();
var server = require('http').Server(app);

var LINEBot = require('line-messaging');
var request = require('request');
var rp = require('request-promise');

var bot = LINEBot.Client({
  channelID: '15', //edited
  channelSecret: '1c63', // edited
  channelAccessToken: 'Fej+fG2APtoM9jFgCK/1zp8dXM2n1XnhxYt+lQyUS68+R+Qp6lZV8VFdaQ89yUx9ELVktzn9f3z9Bim5lMjWMWc13xOeQdB04t89/1O/w1cDnyilFU=' // edited
}, server);

app.use(bot.webhook('/'));

bot.on(LINEBot.Events.MESSAGE, function(replyToken, message) {
  // add code below
  console.log('dfsdfsdfsdfsdfsdfsdfdsfdsfsdfsdfsdfsdfsdfsdf' + replyToken);
});
server.listen(process.env.PORT || 5000, () => {
    console.log(`server is listeninssssg to ${process.env.PORT || 5000}...`);
});
snlangsuan commented 6 years ago

If you use line-messaging version 1.2.0 please change code

var app = require('express')();
var LINEBot = require('line-messaging');

var bot = LINEBot.Client({
    channelID: '<your channel ID>',
    channelSecret: '<your channel secret>',
    channelAccessToken: '<your access token>'
});

app.use(bot.webhook('/'));
bot.on(LINEBot.Events.MESSAGE, function(replyToken, message) {
    console.log('messsage', message.getText());
});

app.listen(8080);

I'm sorry, I not change README.md some code. Thank you.

jpaul55 commented 6 years ago

Awesome! Thank you for the reply. It works now. however how can I set a condition for the replyTextMessage.

this code doesnt work if I put an if condition inside bot.on event.

bot.on(LINEBot.Events.MESSAGE, function(replyToken, message) {
  // add code below.
 console.log('messsage', message.getText());
 console.log(replyToken)
var resp;
if(message.getText() == "hello world"){
  resp = "hi your awesome";
}else{
 resp = "still awesome";
}
 bot.replyTextMessage(replyToken, resp).then(function(data) {
  // add your code when success.
  console.log(data);
}).catch(function(error) {
  // add your code when error.
 });
});
snlangsuan commented 6 years ago

Please check settings on your console https://developers.line.me/console/ my code (sent 'hello world' and received 'hi your awesome') and I test it.

var msg = message.getText();
var resp = '';
if (msg === 'hello world') {
    resp = 'hi your awesome';
} else {
    resp = 'still awesome';
}

bot.replyTextMessage(replyToken, resp).then((data) => {
    console.log(data);
}).catch((error) => {
    console.error(error);
});
jpaul55 commented 6 years ago

Thank you snlangsuan. Im able to see the message now, however replyToken has a limit? because sometimes it works and sometimes it doesnt. This is the reply from my logs.

2018-02-12T07:24:32.119364+00:00 app[web.1]: LineBot is running.
2018-02-12T07:24:32.571231+00:00 heroku[web.1]: State changed from starting to up
2018-02-12T07:24:39.833955+00:00 heroku[router]: at=info method=POST path="/" host=line-botpoc-demo.herokuapp.com request_id=8394cc97-a3ea-4915-9780-e1fcddf379b9 fwd="203.104.146.152" dyno=web.1 connect=1ms service=14ms status=200 bytes=117 protocol=https
2018-02-12T07:24:39.829761+00:00 app[web.1]: webhook request
2018-02-12T07:24:50.787937+00:00 heroku[router]: at=info method=POST path="/" host=line-botpoc-demo.herokuapp.com request_id=b96d586f-0091-4b85-a4fe-d72d57d53c84 fwd="203.104.146.152" dyno=web.1 connect=1ms service=4ms status=200 bytes=117 protocol=https
2018-02-12T07:24:50.785957+00:00 app[web.1]: webhook request
2018-02-12T07:26:38.521184+00:00 heroku[router]: at=info method=POST path="/" host=line-botpoc-demo.herokuapp.com request_id=7923dc70-8051-4e7f-9526-807ba704d60d fwd="203.104.146.152" dyno=web.1 connect=1ms service=1ms status=200 bytes=117 protocol=https
2018-02-12T07:26:38.520482+00:00 app[web.1]: webhook request

and this is the log for the messages that works

2018-02-12T07:08:13.067934+00:00 app[web.1]: LineBot is running.
2018-02-12T07:08:13.329222+00:00 heroku[web.1]: State changed from starting to up
2018-02-12T07:08:37.325247+00:00 heroku[router]: at=info method=POST path="/" host=line-botpoc-demo.herokuapp.com request_id=fb998079-994c-4f3b-8ce7-17139c389cf0 fwd="203.104.146.152" dyno=web.1 connect=0ms service=13ms status=200 bytes=117 protocol=https
2018-02-12T07:08:37.318684+00:00 app[web.1]: webhook request
2018-02-12T07:08:40.312502+00:00 heroku[router]: at=info method=POST path="/" host=line-botpoc-demo.herokuapp.com request_id=f952973b-d0ad-4138-b735-a4b3b66eac99 fwd="203.104.146.152" dyno=web.1 connect=0ms service=7ms status=200 bytes=117 protocol=https
2018-02-12T07:08:40.310516+00:00 app[web.1]: webhook request
2018-02-12T07:08:40.314660+00:00 app[web.1]: messsage Ghhh
2018-02-12T07:08:49.510340+00:00 heroku[router]: at=info method=POST path="/" host=line-botpoc-demo.herokuapp.com request_id=d30b8c4c-dbb6-4708-b250-fb1d7343c7c6 fwd="203.104.146.152" dyno=web.1 connect=0ms service=3ms status=200 bytes=117 protocol=https
snlangsuan commented 6 years ago

It replyToken becomes invalid after a certain period of time, If you has process (long time) before sent message, Please use:

var userId = message.getUserId();
bot.pushTextMessage(userId, 'hello!');

You can find more information from Messaging API

jpaul55 commented 6 years ago

hi snlangsuan, using message.getUserId() doesnt work all the time. maybe this has something to do with my webhook because sometimes it works and sometimes it does not,

snlangsuan commented 6 years ago

It module follows the LINE Messaging API, Some action not response userId