如何解决Go中的电报漫游器未收到ResponseWriter中包含的答案
我正在尝试在无服务器站点(例如Vercel)中托管漫游器电报。我实质上是在引导this tutorial;目前,我正在本地托管它,并使用ngrok将其绑定到机器人。该程序是这样的...
package handler
import (
"fmt"
"net/http"
"time"
"encoding/json"
"github.com/go-telegram-bot-api/telegram-bot-api"
"log"
"io/ioutil"
)
type Hito struct {
URI string
Title string
fecha time.Time
}
type Response struct {
Msg string `json:"text"`
ChatID int64 `json:"chat_id"`
Method string `json:"method"`
}
var hitos = []Hito {
Hito {
URI: "0.Repositorio",Title: "Datos básicos y repo",fecha: time.Date(2020,time.September,29,11,30,time.UTC),},Hito {
URI: "5.Serverless",Title: "Trabajando con funciones serverless",time.November,22,}
func Handler(w http.ResponseWriter,r *http.Request) {
defer r.Body.Close()
body,_ := ioutil.ReadAll(r.Body)
var update tgbotapi.Update
if err := json.Unmarshal(body,&update); err != nil {
log.Fatal("Error en el update →",err)
}
log.Printf("[%s] %s",update.Message.From.UserName,update.Message.Text)
currentTime := time.Now()
var next int
var queda time.Duration
for indice,hito := range hitos {
if ( hito.fecha.After( currentTime ) ) {
next = indice
queda = hito.fecha.Sub( currentTime )
}
}
if update.Message.IsCommand() {
text := ""
switch update.Message.Command() {
case "kk":
if ( next > 0 ) {
text = queda.String()
} else {
text = "Ninguna entrega próxima"
}
default:
text = "No me sé ese comando"
}
msg := fmt.Sprintf("{\"text\": \"%s\",%d,\"method\":\"sendMessage\"}",text,update.Message.Chat.ID,)
log.Printf("JSON %s",msg)
fmt.Fprintf(w,msg)
}
}
整个代码为here。无论如何,问题似乎出在最后一条语句中:fmt.Fprintf(w,msg)
应该将响应打印到Golang http.ResponseWriter,但它永远不会到达目的地。日志显示的JSON似乎还可以
{"text": "232h47m23.215314069s",14974002,"method":"sendMessage"}
我不确定在哪里可以访问Telegram的响应,或者访问Telegram中的日志以查看实际发送的内容,或者我是否正确地进行了整个操作。知道从哪里开始寻找吗?
更新:在注释中,有人指出它似乎不是有效的JSON;但是,这是原始教程中使用的格式,我似乎找不到该值的正确字段名称。
更新2 :已测试另一个JSON值:
{"text": "232h32m43.981011101s","chat_id": "14974002","method":"sendMessage"}
相同的结果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。