Random values, dates and hashes
Display names with CommonLib, generate random values, format UTC dates, and calculate hashes using BJS utilities.
These are separate tasks with different tools. JavaScript's Math and Date, plus runtime CryptoJS, are available without a Store library. Random and DateTimeFormat are installed libraries.
Display a user's name
CommonLib is included in BJS. It is the shorter name for Libs.CommonLib and Libs.commonLib; no installation is required.
if (!user) { return; }
const name = CommonLib.getNameFor(user) || "there";
Bot.sendMessage("Hello, " + name + "!", { parse_mode: null });The helper prefers @username, then the first name, then the last name, and returns an empty string when none is available. The fallback above uses there in that case. It chooses display text, not an authorization identity.
Choose a random reply
const replies = ["Hello!", "Welcome!", "Good to see you!"];
const index = Math.floor(Math.random() * replies.length);
Bot.sendMessage(replies[index]);For a die result:
const roll = 1 + Math.floor(Math.random() * 6);
Bot.sendMessage("You rolled " + roll);Use this for casual variation, not secret tokens or decisions requiring cryptographic randomness.
Format a date
For an unambiguous timestamp:
For a custom UTC display, install DateTimeFormat:
In this library, lowercase mm means month and uppercase MM means minutes. The third argument selects UTC. An invalid date throws. Do not infer the user's timezone from the server clock; store an explicit preference if your bot needs local-time schedules.
Calculate a hash or HMAC
CryptoJS is provided by the runtime:
An HMAC uses a shared secret:
For provider callbacks, use the exact required algorithm, original payload bytes and output encoding. Re-serializing parsed JSON may change the signature. Hashing is not encryption; an ordinary hash of a password is not a password storage scheme. See the CryptoJS reference for supported encodings and methods.
For an actual external callback flow, continue with Webhooks or OxaPay.
Last updated
Was this helpful?