Beispiel #1
0
func sendNotify(rmsg pmsg.RouteMsg) bool {
	if pusher != nil {
		var msg *Msg
		message := map[string]json.RawMessage{}
		err := json.Unmarshal(rmsg.Body(), &message)
		if msg, err = ConvertMsg(message); err != nil {
			ERROR.Println(err)
			return false
		}
		if err != nil {
			ERROR.Println(err)
			return false
		}

		if msg.To == 0 || msg.Bodies == nil {
			return true
		}

		var pushContent string
		var sender string
		if msg.Sender == nil {
			sender = "nobody"
		} else {
			sender = *msg.Sender
		}
		for _, body := range *msg.Bodies {
			switch body.GetType() {
			case TextMsgBodyType:
				pushContent = fmt.Sprintf("%s: %s", sender, body.(*ContentMsgBody).Content)
			case ImageMsgBodyType:
				pushContent = fmt.Sprintf("%s sent you a photo.", sender)
			case SoundMsgBodyType:
				pushContent = fmt.Sprintf("%s sent you a voice message.", sender)
			case LocationMsgBodyType:
				pushContent = fmt.Sprintf("%s sent you a location.", sender)
			case StickMsgBodyType:
				pushContent = fmt.Sprintf("%s: [sticker]", sender)
			default:
				return false
			}
		}
		token := gettokens(msg.To)
		if len(token) == 0 {
			return true
		}
		for _, tk := range token {
			if tk == "" {
				continue
			}
			tks := strings.Split(tk, ":")
			if len(tks) != 2 {
				ERROR.Println("unkonwn token", tk)
				continue
			}
			var dev int
			var err error
			if dev, err = strconv.Atoi(tks[0]); err != nil {
				ERROR.Println("unkonwn token", tk)
				continue
			}

			pusher.Push(byte(dev), tks[1], pushContent)
		}
	}
	return true
}
Beispiel #2
0
func sendNotify(rmsg pmsg.RouteMsg) bool {
	if pusher != nil {
		var msg Msg
		err := json.Unmarshal(rmsg.Body(), &msg)
		if err != nil {
			ERROR.Println(err)
			return false
		}
		if msg.To == nil {
			return true
		}

		var pushContent string
		var sender string
		if msg.Sender == nil {
			sender = "nobody"
		} else {
			sender = *msg.Sender
		}
		if msg.Option == 1 {
			pushContent = fmt.Sprintf("%s sent you a Flash Message.", sender)
		} else {
			switch msg.Type {
			case TextMsgType:
				if msg.Content == nil {
					return false
				}
				pushContent = fmt.Sprintf("%s: %s", sender, *msg.Content)
			case ImageMsgType:
				pushContent = fmt.Sprintf("%s sent you a photo.", sender)
			case SoundMsgType:
				pushContent = fmt.Sprintf("%s sent you a voice message.", sender)
			case LocationMsgType:
				pushContent = fmt.Sprintf("%s sent you a location.", sender)
			case StickMsgType:
				pushContent = fmt.Sprintf("%s: [sticker]", sender)
			case NotifyMsgType:
				if msg.Content == nil || len(*msg.Content) == 0 {
					return false
				}
				pushContent = *msg.Content
			default:
				return false
			}
		}

		token := gettokens(*msg.To)
		for _, tk := range token {
			if tk == "" {
				continue
			}
			tks := strings.Split(tk, ":")
			if len(tks) != 2 {
				ERROR.Println("unkonwn token", tk)
				continue
			}
			var dev int
			var err error
			if dev, err = strconv.Atoi(tks[0]); err != nil {
				ERROR.Println("unkonwn token", tk)
				continue
			}

			pusher.Push(byte(dev), tks[1], pushContent)
		}
	}
	return true
}