From 768895c4b2c39e0dcc0547f5ff518cb7a3933c63 Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 21 Jul 2025 23:38:09 +0200 Subject: [PATCH] Mon 21 Jul 22:43:21 CEST 2025 --- .../botui/examples/git-stars-bot/stars-bot.js | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 js/ui/botui/examples/git-stars-bot/stars-bot.js diff --git a/js/ui/botui/examples/git-stars-bot/stars-bot.js b/js/ui/botui/examples/git-stars-bot/stars-bot.js new file mode 100644 index 0000000..b7af618 --- /dev/null +++ b/js/ui/botui/examples/git-stars-bot/stars-bot.js @@ -0,0 +1,49 @@ +var loadingMsgIndex, + botui = new BotUI('stars-bot'), + API = 'https://api.github.com/repos/'; + +function sendXHR(repo, cb) { + var xhr = new XMLHttpRequest(); + var self = this; + xhr.open('GET', API + repo); + xhr.onload = function () { + var res = JSON.parse(xhr.responseText) + cb(res.stargazers_count); + } + xhr.send(); +} + +function init() { + botui.message + .bot({ + delay: 1000, + content: 'Enter the repo name to see how many stars it have:' + }) + .then(function () { + return botui.action.text({ + delay: 1000, + action: { + value: 'moinism/botui', + placeholder: 'moinism/botui' + } + }) + }).then(function (res) { + loadingMsgIndex = botui.message.bot({ + delay: 200, + loading: true + }).then(function (index) { + loadingMsgIndex = index; + sendXHR(res.value, showStars) + }); + }); +} + +function showStars(stars) { + botui.message + .update(loadingMsgIndex, { + content: 'it has !(star) ' + (stars || "0") + ' stars.' + }) + .then(init); // ask again for repo. Keep in loop. +} + +init();