Ejemplo n.º 1
0
func (rop *RepeatOrderProcessor) ProcessMessage(in *s.InPkg) *s.MessageResult {
	lastOrder, err := rop.Storage.Orders.GetByOwnerLast(in.From, rop.CompanyName)
	if err != nil {
		return m.DB_ERROR_RESULT
	}
	if lastOrder != nil {
		order, err := NewCoffeeOrderFromMap(lastOrder.OrderData.Content)
		if err != nil {
			log.Printf("CB Error at forming new coffee order %v", err)
			return m.MESSAGE_DATA_ERROR_RESULT
		}
		orderId := u.GenIntId()
		err = rop.Storage.Orders.AddOrderObject(db.OrderWrapper{
			OrderId:   orderId,
			When:      time.Now(),
			Whom:      in.From,
			Source:    rop.CompanyName,
			Active:    true,
			OrderData: order.ToOrderData(),
		})
		if err != nil {
			log.Printf("CB Error at storing repeated order %v", err)
			return m.DB_ERROR_RESULT
		}
		err = rop.Storage.Messages.StoreMessageObject(db.MessageWrapper{
			MessageID:         in.Message.ID,
			From:              in.From,
			To:                rop.CompanyName,
			Body:              "Мне бы повторить...",
			Unread:            1,
			NotAnswered:       1,
			Time:              time.Now(),
			TimeStamp:         time.Now().Unix(),
			TimeFormatted:     time.Now().Format(time.Stamp),
			Attributes:        []string{"coffee"},
			AdditionalData:    order.ToAdditionalMessageData(),
			AdditionalFuncs:   getAdditionalFuncs(orderId, rop.CompanyName, in.From, in.GetMessageId()),
			RelatedOrderState: "Отправлено в кофейню",
			RelatedOrder:      orderId,
		})
		if err != nil {
			log.Printf("CB Error at storing message for order %v", err)
			return m.DB_ERROR_RESULT
		}
		cmds, err := rop.CommandsFunc(in)
		if err != nil {
			log.Printf("CB Error at forming commands %v", err)
		}
		return &s.MessageResult{Body: "Ваш заказ повторен!", Commands: cmds}
	}
	return &s.MessageResult{Body: "Нечего повторять."}
}
Ejemplo n.º 2
0
func (odp *OrderBakeProcessor) ProcessMessage(in *s.InPkg) *s.MessageResult {
	if in.UserData != nil && in.Message != nil && in.Message.Commands != nil {
		err := odp.Storage.Users.StoreUserData(in.From, in.UserData)
		if err != nil {
			return m.DB_ERROR_RESULT
		}
		commands := *(in.Message.Commands)
		for _, command := range commands {
			if command.Action == "order_bake" && command.Form.Name == "order_bake_form" {
				order, err := NewCoffeeOrderFromForm(command.Form)
				if err != nil {
					log.Printf("COFFEE BOT error at forming order from form: %v", err)
					return m.MESSAGE_DATA_ERROR_RESULT
				}
				orderId := u.GenIntId()
				err = odp.Storage.Orders.AddOrderObject(db.OrderWrapper{
					OrderId:   orderId,
					When:      time.Now(),
					Whom:      in.From,
					Source:    odp.CompanyName,
					Active:    true,
					OrderData: order.ToOrderData(),
				})
				if err != nil {
					log.Printf("CB Error at storing bake order %v", err)
					return m.DB_ERROR_RESULT
				}
				err = odp.Storage.Messages.StoreMessageObject(db.MessageWrapper{
					MessageID:         in.Message.ID,
					From:              in.From,
					To:                odp.CompanyName,
					Body:              "Заказ выпечки!",
					Unread:            1,
					NotAnswered:       1,
					Time:              time.Now(),
					TimeStamp:         time.Now().Unix(),
					TimeFormatted:     time.Now().Format(time.Stamp),
					Attributes:        []string{"coffee"},
					AdditionalData:    order.ToAdditionalMessageData(),
					AdditionalFuncs:   getAdditionalFuncs(orderId, odp.CompanyName, in.From, in.GetMessageId()),
					RelatedOrderState: "Отправлено в кофейню",
					RelatedOrder:      orderId,
				})
				if err != nil {
					log.Printf("CB Error at storing bake message %v", err)
					return m.DB_ERROR_RESULT
				}
				cmds, err := odp.CommandsFunc(in)
				if err != nil {
					return s.ErrorMessageResult(err, cmds)
				}
				return &s.MessageResult{
					Commands: cmds,
					Type:     "chat",
					Body:     "Ваш заказ создан!",
				}
			}

		}
	}
	return m.MESSAGE_DATA_ERROR_RESULT
}