示例#1
0
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
func HandleEvent() {
	tkn := os.Getenv("SLACK_BOT_TOKEN")
	api := slack.New(tkn)
	// api.SetDebug(true)
	ctxs.NewContext(&ags.LoadPattern{}).Exec()

	rtm := api.NewRTM()
	go rtm.ManageConnection()
	go cron.Init(rtm, api)

Loop:
	for {
		select {
		case msg := <-rtm.IncomingEvents:
			// fmt.Print("Event Received: ")
			switch ev := msg.Data.(type) {
			case *slack.HelloEvent:
				// Ignore hello

			case *slack.ConnectedEvent:
				// fmt.Println("Infos:", ev.Info)
				users, _ := api.GetUsers()
				store.MakeUserMap(users)
				channels, _ := api.GetChannels(true)
				store.MakeChanMap(channels)
				msg := rtm.NewOutgoingMessage(
					"あーえー気分やわ",
					store.ChanByName("general").ID,
				)
				rtm.SendMessage(msg)

			case *slack.MessageEvent:
				fmt.Printf("Message: %v\n", ev)
				go mh.HandleMsg(ev, rtm, api)

			case *slack.PresenceChangeEvent:
				fmt.Printf("Presence Change: %v\n", ev)

			case *slack.LatencyReport:
				// fmt.Printf("Current latency: %v\n", ev.Value)

			case *slack.RTMError:
				fmt.Printf("Error: %s\n", ev.Error())

			case *slack.InvalidAuthEvent:
				fmt.Printf("Invalid credentials")
				break Loop

			default:

				// Ignore other events..
				// fmt.Printf("Unexpected: %v\n", msg.Data)
			}
		}
	}
}
示例#3
0
func (eh *WakaranErrHandler) HandleError(ctx coa.Context, err error) error {
	if err == ErrNonError {
		return err
	}
	err = (&DefaultErrorHandler{}).HandleError(ctx, err)
	mctx := ctx.(*ctxs.MsgContext)
	chanName := mctx.Msg.Channel
	rtm := mctx.RTM
	rtm.SendMessage(rtm.NewOutgoingMessage("すまん。わからんわ", store.ChanByName(chanName).ID))
	return err
}
示例#4
0
func (ag *AddPattern) HandleError(ctx coa.Context, err error) error {
	if err == eh.ErrNonError {
		return err
	}
	err = (&eh.DefaultErrorHandler{}).HandleError(ctx, err)
	mctx := ctx.(*ctxs.MsgContext)
	chanName := mctx.Msg.Channel
	rtm := mctx.RTM
	rtm.SendMessage(rtm.NewOutgoingMessage("Failed to add pattern.", store.ChanByName(chanName).ID))
	return err
}
示例#5
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
}