DEV Community

Discussion on: Sending images and more with Telegram bot

Collapse
 
nileshkawar profile image
Nilesh Kawar

So basically I have an array i want to show elements from an array in inlineKeyboard buttons

I have explained it on stackoverflow:
stackoverflow.com/questions/676461...

Pls help

Thread Thread
 
rizkyrajitha profile image
Rajitha Gunathilake

I'll take a look , and let you know .

Thread Thread
 
nileshkawar profile image
Nilesh Kawar

Yes please

Thread Thread
 
rizkyrajitha profile image
Rajitha Gunathilake
const { Telegraf } = require("telegraf");
const axios = require("axios");

const bot = new Telegraf("XXXXX");
bot.start((ctx) => ctx.reply("Welcome"));
bot.help((ctx) => ctx.reply("Send me a sticker"));
bot.on("sticker", (ctx) => ctx.reply("👍"));
bot.hears("hi", (ctx) => ctx.reply("Hey there"));
bot.hears("Wow", async (ctx) => {
  let stateNames = await getStates();
  console.log(stateNames);

  let listt = [];

  for (let index = 0; index < stateNames.length - 1; index += 2) {
    listt.push([
      { text: stateNames[index], callback_data: String(index) },
      { text: stateNames[index + 1], callback_data: String(index + 1) },
    ]);
  }

  console.log(listt);

  ctx.telegram.sendMessage(ctx.chat.id, "nani", {
    reply_markup: {
      inline_keyboard: listt,
    },
  });
});

bot.launch();

// Enable graceful stop
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));

async function getStates() {
  url = "https://api.covid19india.org/data.json";
  res = await axios.get(url);

  stateArr = res.data.statewise;
  totalStates = stateArr.length;

  let stateName = new Array();

  for (let i = 0; i < totalStates; i++) {
    stateName[i] = stateArr[i].state;
  }

  // console.log(stateName);

  return stateName;
}

Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
rizkyrajitha profile image
Rajitha Gunathilake

Thread Thread
 
rizkyrajitha profile image
Rajitha Gunathilake

hope this helps

Thread Thread
 
nileshkawar profile image
Nilesh Kawar

Thank you so much, sir. I have been stuck for one week on this issue. Thank you so much for resolving and providing a solution.

I tried myself but couldn't solve it. I googled it, asked so many peoples but no one replied, and finally, you solved it.
Again Thank you so much.

Thread Thread
 
rizkyrajitha profile image
Rajitha Gunathilake

your welcome ✌