WhatsApp Bot: Custom Welcome Message on Group Join - Aris's Arrival
This code implements a custom welcome message for a WhatsApp bot when a specific user joins a group. It leverages the 'before' function in the WhatsApp Bot SDK to execute the logic.
Code Breakdown
The code snippet is written in JavaScript and utilizes the 'before' function from the WhatsApp Bot SDK. The 'before' function is executed before any message is processed by the bot.
-
Initial Setup:
- The code defines a variable
conn.danil_jointhat stores a state indicating whether 'Aris' has joined the group and the timestamp of the last join event. If the variable is undefined, it's initialized with the default state ofjoin: falseandtime: 0.
- The code defines a variable
-
Group Join Check:
- The code checks if the message is in a group and if the last join time of 'Aris' is not greater than the current time. This ensures that the welcome message is only sent once after 'Aris' joins the group.
-
Welcome Message Logic:
- If the message sender is 'Aris' (
m.sender === '62895423189239@s.whatsapp.net'), the following actions are performed:- A welcome message is sent to the group chat using the
conn.sendMessagefunction. This message includes a personalized welcome for 'Aris'. - The
conn.danil_joinstate is updated to reflect the latest join event. Thejoinproperty is set totrue, and thetimeproperty is set to the current time plus 2 seconds.
- A welcome message is sent to the group chat using the
- If the message sender is 'Aris' (
Example Usage
The code can be integrated into your WhatsApp bot by placing it within the 'before' function handler. When a group message is received, the bot will check if the sender is 'Aris'. If it is, the welcome message will be sent to the group chat.
Conclusion
This code snippet demonstrates a basic implementation of a custom welcome message for a WhatsApp bot. You can modify and expand upon this example to create more personalized and sophisticated welcome messages for your bot.
原文地址: https://www.cveoy.top/t/topic/o7Gr 著作权归作者所有。请勿转载和采集!