drlnsli18864734 2017-01-29 10:02
浏览 48
已采纳

如何在golang中没有主体的情况下扩展功能

I want to user the ippy04/messengerbot library to build a bot for the facebook messenger.

For receiving a new message the library uses a construct I can´t wrap my head around. The following function type is defined (but without body) in the relevant library source file:

type MessageReceivedHandler func(*MessengerBot, Event, MessageOpts, ReceivedMessage)

This type then gets attached to the actual bot:

type MessengerBot struct {
    MessageReceived  MessageReceivedHandler
}

Later in the code it gets called like this:

if bot.MessageReceived != nil {
  go bot.MessageReceived(bot, entry.Event, message.MessageOpts, *message.Message)
}

Now it seems I need to extend MessageReceivedHandler with an actual body implementation in my own package. I tried a few thing.

Following another SO thread I did this:

import "github.com/ippy04/messengerbot"
type myMRH messengerbot.MessageReceivedHandler
func (mr myMRH) HRM() {
    log.Println("works!")
}

... but that code never gets called.

Also I tried to extend bot.MessageReceived like this (I´m using GinGonic)

router.POST("/webhook", func(c *gin.Context) {
    bot := messengerbot.NewMessengerBot(os.Getenv("FB_PAGE_ACCESS_TOKEN"), os.Getenv("FB_MESSENGER_VERIFY_TOKEN"))
    bot.Debug = true
    bot.MessageReceived = func(*MessengerBot, Event, MessageOpts, ReceivedMessage) {
        log.Println("works!")
    }
    bot.Handler(c.Writer, c.Request)
})

but then I have no idea where to get the necessary function variables from since they ar enot exposed by the library.

Any idea on how to implement that MessageReceivedHandler?


Based on the answer from @mykola here is the complete solution to my problem:

router.POST("/webhook", func(c *gin.Context) {
    bot := messengerbot.NewMessengerBot(os.Getenv("FB_PAGE_ACCESS_TOKEN"), os.Getenv("FB_MESSENGER_VERIFY_TOKEN"))
    bot.Debug = true
    bot.MessageReceived = func(bot *messengerbot.MessengerBot, evt messengerbot.Event, opts messengerbot.MessageOpts, msg messengerbot.ReceivedMessage) {
        log.Println(msg.Message.Text)
    }
    bot.Handler(c.Writer, c.Request)
})
  • 写回答

1条回答 默认 最新

  • 普通网友 2017-01-29 10:18
    关注

    The fact that someone somewhere has declared a new type derived from messengerbot.MessageReceivedHandler doesn't and shouldn't have any effect on the bot library itself.

    What you need is to set the handler of the bot either at the construction point, by instantiating it yourself, or later by doing

    bot.MessageReceived = func(bot *MessengerBot, evt Event, opts MessageOpts, msg ReceivedMessage) {
      log.Println("works!", msg)
    }
    

    P.S. You might want to check the tour of go if you haven't yet, as you seem to miss some basic concepts of working with go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献