예제 #1
0
파일: send_msg.go 프로젝트: kazukgw/takobot
func (a *SendMsg) Do(ctx coa.Context) error {
	log.Action("==> send msg")
	rtm := ctx.ActionGroup().(HasRTM).RTM()
	fmt.Printf("send msg to channel: %v msg: %v\n", a.Channel, a.Msg)
	rtm.SendMessage(rtm.NewOutgoingMessage(a.Msg, store.ChanByName(a.Channel).ID))
	return nil
}
예제 #2
0
파일: scrape.go 프로젝트: kazukgw/takobot
func (a *Scrape) Do(ctx coa.Context) error {
	log.Action("==> scrape")
	doc, err := gq.NewDocument(a.URL)
	if err != nil {
		return err
	}
	a.Document = doc
	return nil
}
예제 #3
0
func (a *SendAttachments) Do(ctx coa.Context) error {
	log.Action("==> send attachments")
	client := ctx.ActionGroup().(HasClient).Client()
	params := slack.PostMessageParameters{}
	params.Attachments = []slack.Attachment{a.Attachment}
	params.Username = "******"
	params.IconURL = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT4Nm9GTh-1aTRmNMdkkAYoCMFHALSj560lxbHA7nYSYjBcptH0JA"
	chanID := store.ChanByName("general").ID
	_, _, err := client.PostMessage(chanID, "", params)
	if err != nil {
		return err
	}
	return nil
}
예제 #4
0
func (a *MsgWithPattern) Do(ctx coa.Context) error {
	log.Action("==> msg with pattern")
	msgs := make([]string, 0)
	for re, ms := range a.Patterns {
		if re.Match([]byte(a.Source)) {
			msgs = append(msgs, ms...)
		}
	}
	if len(msgs) == 0 {
		msgs = []string{"すまん。わからんわ", "ちょいむずかしいわ"}
	}
	rand.Seed(time.Now().UnixNano())
	a.ResultMsg = msgs[rand.Intn(len(msgs))]
	return nil
}
예제 #5
0
func (a *MsgHistory) Do(ctx coa.Context) error {
	log.Action("==> msg history")
	ag := ctx.ActionGroup()
	db := ag.(db.HasDB).DB()
	if a.Scope != nil {
		db = db.Scopes(a.Scope)
	}
	if a.LastMinutes > 0 {
		db.Where(
			"created_at > ?",
			time.Now().Add(time.Duration(-1*a.LastMinutes)*time.Minute),
		).Find(&a.Msgs)
		return nil
	}

	if a.LastMsgNum > 0 {
		db.Order("id desc").Limit(a.LastMsgNum).Find(&a.Msgs)
	}
	return nil
}
예제 #6
0
func (ag *GetRTMAndSendMsg) Do(ctx coa.Context) error {
	log.Action("==> get rtm and send msg")
	return nil
}
예제 #7
0
func (a *GetMsgFromCtx) Do(ctx coa.Context) error {
	log.Action("==> get msg from context")
	mctx := ctx.(*ctxs.MsgContext)
	a.Msg = mctx.Msg
	return nil
}
예제 #8
0
func (a *GetClient) Do(ctx coa.Context) error {
	log.Action("==> get client")
	mctx := ctx.(*ctxs.MsgContext)
	a.client = mctx.Client
	return nil
}
예제 #9
0
파일: get_rtm.go 프로젝트: kazukgw/takobot
func (a *GetRTM) Do(ctx coa.Context) error {
	log.Action("==> get rtm")
	mctx := ctx.(*ctxs.MsgContext)
	a.rtm = mctx.RTM
	return nil
}
예제 #10
0
func (a *Filtering) Do(ctx coa.Context) error {
	log.Action("==> filtering")
	if ag, ok := ctx.ActionGroup().(HasMsg); ok {
		m := ag.GetMsg()
		chanName := store.ChanByID(m.Channel).Name
		userName := store.UserByID(m.User).Name
		toUser := m.ToUser
		if len(a.PermitChannel) > 0 {
			for _, c := range a.PermitChannel {
				if c == chanName {
					log.Info("filtering: permit channel ok")
					return nil
				}
			}
			log.Info("filtering: permit channel bad")
			return eh.ErrNonError
		}

		if len(a.DenyChannel) > 0 {
			for _, c := range a.DenyChannel {
				if c == chanName {
					log.Info("filtering: deny channel bad")
					return eh.ErrNonError
				}
			}
			log.Info("filtering: deny channel ok")
			return nil
		}

		if len(a.FromPermitUser) > 0 {
			for _, u := range a.FromPermitUser {
				if u == userName {
					log.Info("filtering: from permit user ok")
					return nil
				}
			}
			log.Info("filtering: from permit user bad")
			return eh.ErrNonError
		}

		if len(a.FromDenyUser) > 0 {
			for _, u := range a.FromDenyUser {
				if u == userName {
					log.Info("filtering: from deny user bad")
					return eh.ErrNonError
				}
			}
			log.Info("filtering: from deny user ok")
			return nil
		}

		if len(a.ToPermitUser) > 0 {
			if toUser == nil {
				log.Info("filtering: to permit user bad")
				return eh.ErrNonError
			}
			for _, u := range a.ToPermitUser {
				if u == toUser.Name {
					log.Info("filtering: to permit user ok")
					return nil
				}
			}
			log.Info("filtering: to permit user bad")
			return eh.ErrNonError
		}

		if len(a.ToDenyUser) > 0 {
			if toUser == nil {
				log.Info("filtering: to permit user bad")
				return eh.ErrNonError
			}
			for _, u := range a.ToDenyUser {
				if u == toUser.Name {
					log.Info("filtering: to deny user bad")
					return eh.ErrNonError
				}
			}
			log.Info("filtering: to deny user ok")
			return nil
		}

		if a.Ratio > 0.0 {
			if rand.Float64() <= a.Ratio {
				log.Info("filtering: ratio ok")
				return nil
			}
			log.Info("filtering: ratio bad")
			return eh.ErrNonError
		}

		if a.Func != nil && !a.Func(m) {
			log.Info("filtering: func bad")
			return eh.ErrNonError
		}
	}
	log.Info("filtering: ok")
	return nil
}