How to read messages from chat using tmi?

This might seem like an easy one. But I’ve used more than a week trying to get around this problem.
Which is about a bot I wrote in node.js using …

const tmi = require('tmi.js');

How to invoke client.on(‘message’,…)? I’ve declared it in its simplest form, like this (with two console calls for debugging purposes).

client.on('message',(ch,data,msg,self)=>{
  console.log('['+self+'] '+msg);
  console.dir(data);
  if (self) return;
  if (msg==='?') {
    help_menu( );
  }
});

Now, the issue here is it only triggers either when self is true or when message-type is ‘whisper’. It would have been nice, but not essencial, to be able to read commands of type ‘chat’. The program is allowed to send messages to the public chat. And to receive them as such. But not from other users.

More important: I can use client.say() to send messages. But what the bot does is, it creates and communicates password-like secrets to users. These cannot be sent as chat messages, since they are supposed to be private. So I REALLY need the client.whisper() to work.
I’ve spent 4 months working on a feature, where the twitch bot is a small but essencial part. And now I need to get the bot working, before I can launch the whole project.

Any thoughts?

Basically, don’t bother, with Whispers, Bots just cannot reliably send whispers.

There is also nothing you can apply to to improve deliverilty, bot’s just can’t whisper. They may be able to reply to a whisper, so user sends the bot a whisper first and the bot replies.

But a bot cannot initate a “conversation” but it can (generally) paticipate in a user started “conversation”

Ideally, you need something like an External Portal protected by Twitch Auth, or an Extension, which is protected by Twitch Auth.

So the “secrets” are only visible to the right users in either.

An an “External portal” is easily convertable to Twitch Extension anyway.

Than you for the quick reply. I fully understand the limitations on whispering. And I do not intend my bot to initiate any private conversations on twitch. What I can do is, to read whispered commands. What I need is, to reply to them with new whispers.

An an “External portal” is easily convertable to Twitch Extension anyway.

I’m not sure it will solve my problem, as I already have an “external portal” to which users are supposed to be granted access by the bot. That’s why it’s passing secret messages.

A link to any documentation on how such an extension is made would be nice. I am no expert on twitch, which I’ve never used before. I do have some programming skills, though.

From what you describe it’s a flat no go

You need an external portal that viewers/users go to in order to retrived codes/secrets

Seperate to the external portal that streamers use to add your bot to their channel

That is the purpose of Twitch oAuth really, people login to the external portal with their Twitch Account. No secret code needed, jsut go “hey X you can login to the portal now”. Don’t give them a code to “sign up” to the portal just when they login check if they are on the allowed list.

No need to do any code exchangement…

This is essentially how “sub only external portals” work, just instead of checking if they are a sub to x channel, check if they have been granted access on x channel (which is all your secret code stuff is doing anyway)

Extensions start here Extensions | Twitch Developers

OK, thanks! I’ll check it out.

Is there a way to read commands from the chat into the bot? I thought that would be the main purpose of a bot. But for some reason, the client receives nothing.

Refer to the TMI.js documentation for help with TMI.js (which I don’t use(.

Your OP was mainly referring to utilisation of whispers to send secrets to users so I was following/replying to that thread.

I’ve read both the twitch API docs and the - rather fragmented and somewhat rudimentary - TMI docs on the subject. From there it’s clear, both whispers and normal chat messages are supposed to go both ways. This was something I checked with the documentation even before I begun coding for this project.

Your OP was mainly referring to utilisation of whispers

OK, that’s a slight misunderstanding (sorry). What I meant to say was that the “message” event seem to occure only when either self is true or data[“message-type”] equals “whisper”. It’s like the twitch server doesn’t share chat events with my bot client, unless they originate from the bot itself. Why?
The fact that whispers come through is, however, proof of a proper connection from server to client.

Then … there is the issue of whispers. TMI is just a framework for message handling with twitch API. And, as said before, twitch AIP docs state very clearly that PRVMSG is a message type for which twitch suppors both Send and Receive.

Sounds like you don’t have TMI/your code setup right

As if you joined a room then you should be getting messages that other people send.

You can get further help on TMI.js specifically in the TMI.js room on the Libraries discord

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.