How can I sendMessage with reply_markup(telegram bot)

Viewed 52

I deploy my project on heroku using express+request.I can reply text,but failed to reply with markup.I've been working on this proble for almost 2 days,but I can't fix it.

app.post("/",async(req,final)=>{  
  const body=req.body
  console.log(JSON.stringify(body))
  if(body.message){
    const form={
      text:req.body.message.text,
      msgid:req.body.message.message_id,
      uid:req.body.message.from.id,
      chatid:req.body.message.chat.id
    }
    if(form.text.indexOf("/start")!=-1){
      sendMessage(form.chatid,form.msgid,"Welcome")
    }else{
      let reply_markup={}
      reply_markup.inline_keyboard=[]
      let inlineRow=[]
      inlineRow.push({
        text:"one",
        callback_data:"/one:"+body.message.text
      })
      inlineRow.push({
        text:"two",
        callback_data:"/two:"+body.message.text
      })
      let inlineRow1=[]
      inlineRow1.push({
        text:"three",
        callback_data:"/three:"+body.message.text
      })
      inlineRow1.push({
        text:"four",
        callback_data:"/four:"+body.message.text
      })
      reply_markup.inline_keyboard.push(inlineRow)
      reply_markup.inline_keyboard.push(inlineRow1)
      sendMsgButton(form.chatid,"Click a button",reply_markup)
    }
  }
  final.send("SUCCESS")
})
function sendMsgButton(chat_id,text,reply_markup){
  request({
    url:"https://api.telegram.org/botAPITOken/",
    method:"POST",
    form:{
      "method": "sendMessage",
      "chat_id": chat_id,
      "parse_mode":"Markdown",
      "reply_markup":reply_markup,
      "text":text
    }
  },(err,res,body)=>{
    console.log("SendMessage",body)
  })
}

Someone knows what's wrong with my code? Please help me/(ㄒoㄒ)/~~

1 Answers

Your request to API should have next format:

{
  chat_id: data.chatId,
  text: data.text,
  reply_markup: {
    inline_keyboard: [[{ text: 'Stop', callback_data: "string" }]],
  },
}

You should also specify what kind of error you are getting from Telegram API.

Also, there are limitations in callback_data, it cannot be more than 64 bytes. You can use any online service to check the string size you pass into callback data

Also, your URL should like https://api.telegram.org/botAPITOken/sendMessage.

Related