depoio / node-telegram-bot

Client wrapper for Telegram Bot API (Under heavy development)
MIT License
136 stars 30 forks source link

Bot Termination and Startup Issue #50

Closed boseji closed 8 years ago

boseji commented 8 years ago

Hello,

I am trying to use the Library to be able to Stop the BOT app from Node if needed. However next time when I start the bot again it fails to start up.

Here is a code snippet:

case message.text == '/stop':
      bot.sendMessage({
        chat_id: message.chat.id,
        text: 'Goodbye',
      });
      bot.stop();
    break;

This inside the command parser instantiated by:

// Attach event on every received message 
bot.on('message', function (message) {
  parseMessage(message);
});

Where parseMessage(message) is the function that is used to interpret the message as a command.

The package is running normally when there is no /stop command sent. However ones this is sent from Telegram it causes the BOT to stop. Next time when I try to start the application it does not allow the BOT application to start.

Kindly help regarding this.

Warm Regards, Boseji

longstone commented 8 years ago

Hey Boseji In my opinion on a /stop command you're don't supposed to shut down the bot. You should remove the chat subscription from this participant.

boseji commented 8 years ago

I think then we need to send a different command such as /shutdown will that be alright. The problem is that after bot.stop is called the new instance of the bot can't receive any new messages. It still receives the last message that was posted. This means that if I have written an actuation command then the BOT might redo the action. I put my code now on github kindly have a look. https://github.com/boseji/Telegram-bot-example

I will try to replicate this suggestion on the code and let you know.

longstone commented 8 years ago

I doubt, because we won't start a bot for each chat. but maybe @shernshiou can declare whats the meaning? here is an example how i handle the register and notification part

shernshiou commented 8 years ago

@boseji when you called stop, the client will stop polling.

I will look into your code and check.

boseji commented 8 years ago

Thanks for your help. I have currently removed the Stop part as the code was crashing out. Still need to test it. Will do a trial tonight.

In my case one more observation is that when my telegram_bot.js is crashing if I run the telegram_getuserid_bot.js then the program starts working again. Kindly look at both the JS scripts.

longstone commented 8 years ago

I think your code is broken, not the 'node-telegram-bot'. So this is not a bug of the library. For example, you're using switch case the wrong way see here and how a switch case should be used.

Again this is not a bug of this package!

boseji commented 8 years ago

Its not a bug, may be it looks like.

The switch is a standard trick used in JS for replacing cascaded if-else ladders. Very common in my browser app implementations.

Well I will make it more aligned to actual switch function and try one more time.

longstone commented 8 years ago

Sorry I was really confused, now I looked into your code. You can't run the same bot (apikey) simultan. I tried this right now, and im think you'll get the following logs:

starting getuserid

longstone-box-00:Telegram-bot-example longstone$ node telegram_getuserid_bot.js 
BOT ready!

**starting the bot***

longstone-box-00:Telegram-bot-example longstone$ node telegram_bot.js 
BOT ready!

result getuserid

events.js:146
      throw err;
      ^

Error: Uncaught, unspecified "error" event. (Unknown error.)
    at Bot.emit (events.js:144:17)
    at Request._callback (/Users/longstone/Downloads/9640/untitled folder/Telegram-bot-example/node_modules/node-telegram-bot/lib/Bot.js:232:12)
    at Request.self.callback (/Users/longstone/Downloads/9640/untitled folder/Telegram-bot-example/node_modules/node-telegram-bot/node_modules/request/request.js:198:22)
    at emitTwo (events.js:87:13)
    at Request.emit (events.js:172:7)
    at Request.<anonymous> (/Users/longstone/Downloads/9640/untitled folder/Telegram-bot-example/node_modules/node-telegram-bot/node_modules/request/request.js:1082:10)
    at emitOne (events.js:82:20)
    at Request.emit (events.js:169:7)
    at IncomingMessage.<anonymous> (/Users/longstone/Downloads/9640/untitled folder/Telegram-bot-example/node_modules/node-telegram-bot/node_modules/request/request.js:1009:12)
    at emitNone (events.js:72:20)

I recommend to build the whitelist thing into your Bot, so that you register each call and then manual set the permission to each id.

boseji commented 8 years ago

You need to use the Auth function. Update the User ID from which you would need to communicate with the BOT in the array AuthorizedUsers array line 11 in telegram_bot.js.

To obtain the user ID use the script telegram_getuserid_bot.js This is a measure to ensure that no body else can control the BOT.

boseji commented 8 years ago

I have just updated code the problem still remains. I have changed /start to /begin and /stop to /shutoff Still the problem persists.

The main concern is the call to bot.stop() what are necessary per-conditions for calling this ? Can this be done within a call back or I need to generate a monitor event to be able to turn off the bot.

My goal with the /stop or /shutoff command is that on the responder end I want to kill the daemon. Kindly dwell into this.

shernshiou commented 8 years ago

@boseji can i know your motivation behind calling the stop?

boseji commented 8 years ago

The stop() would terminate the bot connection and hence the program. Its need to be able to shutdown the terminal / BOT program in case some thing goes wrong.

For privacy and security reasons I would like to have the capability to kill the BOT program on the other side from my mobile phone.

boseji commented 8 years ago

Any updates on the async termination of Bot. Still the code remains unchanged.

shernshiou commented 8 years ago

@boseji I am testing 50af69b12fa372862b25dda092269be80617da5c