gocw2/utils.go

48 lines
1.1 KiB
Go
Raw Normal View History

2019-05-03 11:19:20 +02:00
package main
import (
2020-02-05 05:50:15 +01:00
"encoding/json"
2019-05-03 11:19:20 +02:00
"log"
2020-02-05 05:50:15 +01:00
"github.com/Arman92/go-tdlib"
2019-05-03 11:19:20 +02:00
)
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
func logOnError(err error, msg string) {
if err != nil {
log.Printf("%s: %s", msg, err)
}
}
2020-02-05 05:50:15 +01:00
2020-02-05 05:52:11 +01:00
func unmarshalCallbackQueryPayload(rawMsg []byte) (tdlib.CallbackQueryPayload, error) {
2020-02-05 05:50:15 +01:00
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:
2020-02-05 05:52:11 +01:00
var callbackQueryPayloadData tdlib.CallbackQueryPayloadData
2020-02-05 05:50:15 +01:00
err := json.Unmarshal(*rawMsg, &callbackQueryPayloadData)
return &callbackQueryPayloadData, err
case tdlib.CallbackQueryPayloadGameType:
2020-02-05 05:52:11 +01:00
var callbackQueryPayloadGame tdlib.CallbackQueryPayloadGame
2020-02-05 05:50:15 +01:00
err := json.Unmarshal(*rawMsg, &callbackQueryPayloadGame)
return &callbackQueryPayloadGame, err
default:
return nil, fmt.Errorf("Error unmarshaling, unknown type:" + objMap["@type"].(string))
}
}