1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| 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) }) }
|
你对API的请求应该有以下格式:
1 2 3 4 5 6 7
| { chat_id: data.chatId, text: data.text, reply_markup: { inline_keyboard: [[{ text: 'Stop', callback_data: "string" }]], }, }
|
您还应该指定从Telegram API获得的错误类型。此外,回调也有一些限制_数据,不能超过64字节。您可以使用任何在线服务来检查传递给回调数据的字符串大小