为什么我的事件只输入一次数据,但运行两次?

huangapple go评论113阅读模式
英文:

Why does my event run twice when data only fed once?

问题

所以,我已经修整了我的Go代码,以便更好地理解,因为我正在使用库来实现这个功能。

基本上,我正在尝试接收一个JSON文本,将其修整成特定的格式,并将其传递给createTicket函数。

一切都正常工作,我的createTicket函数使用replyText中的正确主题字段创建了一个ticket,但是每当我将一个JSON输入到我的应用程序中时,createTicket函数运行了2-3次,并且我的调试打印消息打印了几次。

在后端,当我只在测试期间一次性输入一个JSON时,createTicket将在我的系统上创建一个ticket,并且会出现2-3个新的ticket。

  1. func handleMessage(bot *lark.Bot, msg *lark.EventV2MessageReceived, client *zendesk.Client) {
  2. query := msg.Message.Content
  3. //fmt.Println(query)
  4. var replyText ReplyText
  5. err := json.Unmarshal([]byte(query), &replyText)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Println(replyText.Text)
  10. client.CreateTicket(context.Background(), zendesk.Ticket{
  11. Subject: replyText.Text,
  12. Priority: "P2",
  13. Comment: zendesk.TicketComment{
  14. Body: "testBody",
  15. },
  16. })
  17. //print("DEBUG: MSG HANDLED AND TICKET CREATED")
  18. }

调用handleMsg()函数的代码是通过以下API调用在我的服务器上监听的。基本上只是一个事件处理程序,所以当我发送一条消息到我的应用程序时,它将进行必要的检查,并将接收到的消息传递给函数,如下所示。

  1. r.POST("/", func(c *gin.Context) {
  2. if evt, ok := middleware.GetEvent(c); ok {
  3. if evt.Header.EventType == lark.EventTypeMessageReceived {
  4. if msg, err := evt.GetMessageReceived(); err == nil {
  5. fmt.Println(msg.Message.Content)
  6. handleMessage(bot, msg, client)
  7. c.JSON(http.StatusOK, gin.H{"status": "ok"})
  8. }
  9. }
  10. }
  11. })
英文:

So i have trimmed my Go code below for better understanding as I am using libraries to implement this.

Basically what I am trying to do is receive a JSON text, trim it into a certain format and pass it over to createTicket function.

Everything works fine and my createTicket function creates it with the correct subject field from replyText, however createTicket runs almost 2-3x everytime I feed a JSON into my application and my debug print message prints few times.

On the backend createTicket will create a ticket on my system and 2-3 new tickets appear when I have only fed the system a JSON only once during my testing.

  1. func handleMessage(bot *lark.Bot, msg *lark.EventV2MessageReceived, client *zendesk.Client) {
  2. query := msg.Message.Content
  3. //fmt.Println(query)
  4. var replyText ReplyText
  5. err := json.Unmarshal([]byte(query), &replyText)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Println(replyText.Text)
  10. client.CreateTicket(context.Background(), zendesk.Ticket{
  11. Subject: replyText.Text,
  12. Priority: "P2",
  13. Comment: zendesk.TicketComment{
  14. Body: "testBody",
  15. },
  16. })
  17. //print("DEBUG: MSG HANDLED AND TICKET CREATED")
  18. }

The code that calls handleMsg() function is through an API call below listening on my server. Basically just an event handler, So when I send a message to my application below it will do the necessary checks and I will pass the message received to the function as below.

  1. r.POST("/", func(c *gin.Context) {
  2. if evt, ok := middleware.GetEvent(c); ok {
  3. if evt.Header.EventType == lark.EventTypeMessageReceived {
  4. if msg, err := evt.GetMessageReceived(); err == nil {
  5. fmt.Println(msg.Message.Content)
  6. handleMessage(bot, msg, client)
  7. c.JSON(http.StatusOK, gin.H{"status": "ok"})
  8. }
  9. }
  10. }
  11. })

答案1

得分: 0

我在 handleMessage(bot, msg, client) 中遗漏了 go。

应该是 go handleMessage(bot, msg, client) 来调用该例程。

这样修复了我的问题。

英文:

I was missing go in handleMessage(bot, msg, client)

It should be go handleMessage(bot, msg, client) to call the routine once.

This fixed my issue.

huangapple
  • 本文由 发表于 2022年3月8日 15:29:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/71391403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定