Difference between revisions of "Bot Playground/Events/chat message"
From SmartBots Developers Docs
(12 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:chat_message}} | {{DISPLAYTITLE:chat_message}} | ||
<onlyinclude>Fires when bot receives a message in the local chat</onlyinclude> | <onlyinclude>Fires when bot receives a message in the local chat</onlyinclude> | ||
+ | |||
+ | <syntaxhighlight lang="javascript"> | ||
+ | Bot.on("chat_message", function(event) { ... }); | ||
+ | </syntaxhighlight> | ||
{{API Event Table}} | {{API Event Table}} | ||
− | {{API Variable Group| | + | {{API Variable Group|''event'' object properties}} |
− | {{API Variable|name}}The name of the event | + | {{API Variable|name}}The name of the event |
− | {{API Variable|speaker_type}}The sender of the message. Can be | + | {{API Variable|speaker_type}}The sender of the message. Can be AGENT or OBJECT |
{{API Variable|speaker_name}}The name of the sender | {{API Variable|speaker_name}}The name of the sender | ||
{{API Variable|speaker_uuid}}The UUID of the sender | {{API Variable|speaker_uuid}}The UUID of the sender | ||
− | {{API Variable|speaker_owner}}The UUID of the owner | + | {{API Variable|speaker_owner}}The UUID of the owner of the sender object. |
{{API Variable|message}}The text of the message | {{API Variable|message}}The text of the message | ||
+ | {{API Variable|chat_type}}One of the following: Normal, Whisper, Shout, OwnerSay | ||
+ | {{API Variable|own_message}}If this message has been said by the bot itself | ||
{{API Variables Table End}} | {{API Variables Table End}} | ||
+ | |||
+ | == Important note == | ||
+ | |||
+ | Bot DOES hear what it says, so you will get a ''chat_message'' event when bot says something in local chat. | ||
+ | |||
+ | Make sure to ignore bot's own messages (especially for auto-responders)! See the example below. | ||
== Example == | == Example == | ||
Line 17: | Line 29: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
Bot.on("chat_message", function(event) { | Bot.on("chat_message", function(event) { | ||
+ | // Ignore own messages | ||
+ | if(event.own_message) { return; } | ||
+ | |||
console.log(event.speaker_name + " says: \n" + event.message); | console.log(event.speaker_name + " says: \n" + event.message); | ||
}); | }); | ||
− | console.log("Bot is listening | + | console.log("Bot is listening for local chat"); |
</syntaxhighlight> | </syntaxhighlight> | ||
{{NavMenu}} | {{NavMenu}} |
Latest revision as of 13:28, 11 December 2023
Fires when bot receives a message in the local chat
Bot.on("chat_message", function(event) { ... });
Reference
This event comes with the following event object:
Variable | Required | Description | |
---|---|---|---|
event object properties: | |||
name | The name of the event | ||
speaker_type | The sender of the message. Can be AGENT or OBJECT | ||
speaker_name | The name of the sender | ||
speaker_uuid | The UUID of the sender | ||
speaker_owner | The UUID of the owner of the sender object. | ||
message | The text of the message | ||
chat_type | One of the following: Normal, Whisper, Shout, OwnerSay | ||
own_message | If this message has been said by the bot itself |
Important note
Bot DOES hear what it says, so you will get a chat_message event when bot says something in local chat.
Make sure to ignore bot's own messages (especially for auto-responders)! See the example below.
Example
Bot.on("chat_message", function(event) {
// Ignore own messages
if(event.own_message) { return; }
console.log(event.speaker_name + " says: \n" + event.message);
});
console.log("Bot is listening for local chat");