edisonchee / slimbot

Telegram Bot API for Node.js
MIT License
223 stars 37 forks source link

sendMessage within other chat bot #23

Closed suntong closed 5 years ago

suntong commented 6 years ago

Hi,

Can I run slimbot.sendMessage alone? Does slimbot.sendMessage need the slimbot.startPolling(); to be working? Ref: https://github.com/edisonchee/slimbot/wiki#getting-started

For e.g., I have this simple script:

const Slimbot = require('slimbot');
const slimbot = new Slimbot(process.env['TELEGRAM_BOT_TOKEN']);
const chatID = process.env['TELEGRAM_CHAT_ID'];

slimbot.sendMessage(chatID, 'Message received')

If I ran it, would it work? How come I got the following?

Unhandled rejection Error: 400:
{"ok":false,"error_code":400,"description":"Bad Request: chat not found"}
    at Request.then.resp (/path/to/node_modules/slimbot/src/telegram.js:43:15)
    at tryCatcher (/path/to/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/path/to/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/path/to/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/path/to/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/path/to/node_modules/bluebird/js/release/promise.js:694:18)
    at _drainQueueStep (/path/to/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/path/to/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/path/to/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/path/to/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:696:18)
    at tryOnImmediate (timers.js:667:5)
    at processImmediate (timers.js:649:5)

All that I want is to use slimbot.sendMessage alone without the slimbot.startPolling(), because I want to send Telegram Message in another chat bot.

Thx

edisonchee commented 6 years ago

Hi @suntong, you don't need slimbot.startPolling() if you don't expect people to interact with your bot.

Your bot could be failing because it was not added to an existing chat group or channel.

suntong commented 6 years ago

Thank you Edison.

Could you try above code with a good chat group please?

I've double-checked that my chat group ID was right. In fact, if I wrap slimbot.sendMessage() into the on meesge event, and add slimbot.startPolling(), then everything is fine. It is only when stripping them out when the problem exist.

Could you give it a try at your end please? Thx.

suntong commented 5 years ago

Hi Edison,

Would you able to duplicate the problem at your end as well, or you are thinking that it's still at me end? Thx

edisonchee commented 5 years ago

Hi @suntong, sorry for the late reply. I had the wrong impression that slimbot.startPolling() is not needed to start sending messages. Sorry about that.

suntong commented 5 years ago

sorry for responding late. I've been out of town and just get back.

Thanks for the confirmation.

Just FTR, slimbot.sendFile works fine without the slimbot.startPolling(). So would it possible for you to create a version of sendMessage that does not depends of the slimbot.startPolling()? thx.

edisonchee commented 5 years ago

I'll start looking into that @suntong. Thanks for the suggestion!

edisonchee commented 5 years ago

@suntong, I just tested slimbot and slimbot.sendMessage works without slimbot.startPolling(). I successfully sent a message to myself through the bot without polling.

suntong commented 5 years ago

Oh, then I must have missed something important. Would you able to share your working code somewhere, like gist? Thanks!

suntong commented 5 years ago

Once again, my simple script and its error message is posted in the first message, @edisonchee

suntong commented 5 years ago

Oh ---,

having upgraded slimbot from  0.8.1 to 0.8.6, the very script posted in the first message is now working. Thanks @edisonchee!