diff --git a/def.go b/def.go index 1db0463..7ce5fab 100644 --- a/def.go +++ b/def.go @@ -45,6 +45,10 @@ type MQKeepAlive struct { Date time.Time `json:"date"` } +type CallbackMsgData struct { + Data `json:"data"` +} + const ( commandForwardMsg = 1 commandReplyMsg = 2 diff --git a/td.go b/td.go index 9c45e43..7377cee 100644 --- a/td.go +++ b/td.go @@ -300,7 +300,7 @@ func clientMsg(c *tdlib.Client, m TGCommand) { b, _ := json.Marshal(d) log.Printf("CallbackQueryPayloadData :\n%s\n", string(b)) log.Printf("Type : %s\n", d.GetCallbackQueryPayloadEnum()) - p, err := tdlib.unmarshalCallbackQueryPayload(b) + p, err := unmarshalCallbackQueryPayload(b) a, err := c.GetCallbackQueryAnswer(m.FromChatID64, m.FromMsgID64, p) logOnError(err, "clientMsg : GetCallbackQueryAnswer") diff --git a/utils.go b/utils.go index 59e408e..536d440 100644 --- a/utils.go +++ b/utils.go @@ -1,7 +1,10 @@ package main import ( + "encoding/json" "log" + + "github.com/Arman92/go-tdlib" ) func failOnError(err error, msg string) { @@ -15,3 +18,30 @@ func logOnError(err error, msg string) { log.Printf("%s: %s", msg, err) } } + +func unmarshalCallbackQueryPayload(rawMsg *json.RawMessage) (tdlib.CallbackQueryPayload, error) { + + if rawMsg == nil { + return nil, nil + } + var objMap map[string]interface{} + err := json.Unmarshal(*rawMsg, &objMap) + if err != nil { + return nil, err + } + + switch tdlib.CallbackQueryPayloadEnum(objMap["@type"].(string)) { + case tdlib.CallbackQueryPayloadDataType: + var callbackQueryPayloadData CallbackQueryPayloadData + err := json.Unmarshal(*rawMsg, &callbackQueryPayloadData) + return &callbackQueryPayloadData, err + + case tdlib.CallbackQueryPayloadGameType: + var callbackQueryPayloadGame CallbackQueryPayloadGame + err := json.Unmarshal(*rawMsg, &callbackQueryPayloadGame) + return &callbackQueryPayloadGame, err + + default: + return nil, fmt.Errorf("Error unmarshaling, unknown type:" + objMap["@type"].(string)) + } +}