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

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

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。

func handleMessage(bot *lark.Bot, msg *lark.EventV2MessageReceived, client *zendesk.Client) {
	query := msg.Message.Content
	//fmt.Println(query)
	var replyText ReplyText
	err := json.Unmarshal([]byte(query), &replyText)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(replyText.Text)

	client.CreateTicket(context.Background(), zendesk.Ticket{
		Subject:  replyText.Text,
		Priority: "P2",
		Comment: zendesk.TicketComment{
			Body: "testBody",
		},
	})

	//print("DEBUG: MSG HANDLED AND TICKET CREATED")
}

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

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

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.

func handleMessage(bot *lark.Bot, msg *lark.EventV2MessageReceived, client *zendesk.Client) {
	query := msg.Message.Content
	//fmt.Println(query)
	var replyText ReplyText
	err := json.Unmarshal([]byte(query), &replyText)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(replyText.Text)

	client.CreateTicket(context.Background(), zendesk.Ticket{
		Subject:  replyText.Text,
		Priority: "P2",
		Comment: zendesk.TicketComment{
			Body: "testBody",
		},
	})

	//print("DEBUG: MSG HANDLED AND TICKET CREATED")
}

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.

r.POST("/", func(c *gin.Context) {
	if evt, ok := middleware.GetEvent(c); ok {
		if evt.Header.EventType == lark.EventTypeMessageReceived {
			if msg, err := evt.GetMessageReceived(); err == nil {
				fmt.Println(msg.Message.Content)
				handleMessage(bot, msg, client)
				c.JSON(http.StatusOK, gin.H{"status": "ok"})
			}
		}
	}
})

答案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:

确定