Пример #1
0
func NewLogger(s *evilbot.SlackBot) error {
	a := ActivityLogger{}
	a.s = s

	var err error
	a.store, err = storage.Load("activity_logger")
	if err != nil {
		return err
	}
	if err := a.store.LoadBucket("all"); err != nil {
		return err
	}
	if err := a.store.LoadBucket("seen"); err != nil {
		return err
	}

	if chs, err := s.RTM.GetChannels(false); err == nil {
		for _, ch := range chs {
			s.RTM.JoinChannel(ch.ID)
			log.Printf("Loading Bucket: %v\n", ch.ID)
			if err := a.store.LoadBucket(ch.ID); err != nil {
				return err
			}
		}
	}
	if err := s.AddEventHandler(
		"default-activity-logger",
		a.ActivityLogHandler,
	); err != nil {
		return err
	}
	if err := s.AddCmdHandler("top5", a.TopFiveHandler); err != nil {
		return err
	}
	if err := s.AddCmdHandler("bottom5", a.BottomFiveHandler); err != nil {
		return err
	}
	if err := s.AddCmdHandler("top10", a.TopTenHandler); err != nil {
		return err
	}
	if err := s.AddCmdHandler("bottom10", a.BottomTenHandler); err != nil {
		return err
	}

	if err := s.AddCmdHandler("seen", a.SeenHandler); err != nil {
		return err
	}

	s.AddCmdHandler("seen", func(e evilbot.Event, br *evilbot.Response) {
		var response string
		if e.Channel != nil {
			log.Printf("Args: %v", e.ArgStr)
			if len(e.ArgStr) > 0 {
				u := strings.Split(e.ArgStr, " ")
				if uInfo, err := br.UserInfo(u[0]); err == nil {
					if t, err := a.Seen(strings.ToLower(uInfo.ID)); err == nil {
						tf := t.Format("Jan 2 2006 03:04 PM EST")
						response = fmt.Sprintf("%v last seen on %v", u[0], tf)
						br.SendToChannel(e.Channel.ID, response)
						return
					}
				} else {
					log.Printf("Error: %v\n", err)
				}
				response = fmt.Sprintf("%v not seen", u[0])
			} else {
				response = "which user?"
			}
		} else {
			response = "only in channel"
		}
		br.ReplyToUser(&e, response)
	})

	s.RegisterEndpoint("/top5/{channelid}", "get", func(rw http.ResponseWriter, r *http.Request, br *evilbot.Response) {
		ch := mux.Vars(r)["channelid"]
		if len(ch) < 0 {
			ch = "all"
		}
		if ch != "all" {
			chInfo, err := br.ChannelInfo(ch)
			if err == nil {
				ch = chInfo.ID
			} else {
				rw.Write([]byte("Evil Bot Says No!"))
				return
			}
		}

		if p, err := a.TopX(ch, 5); err == nil {
			var cname string
			if ch != "all" {
				if cInfo, err := s.RTM.GetChannelInfo(ch); err == nil {
					cname = cInfo.Name
				} else {
					rw.Write([]byte("Evil Bot Says No!"))
					return
				}
			} else {
				cname = "all activity"
			}
			response := map[string]interface{}{
				"channel": cname,
				"results": p,
			}
			for i, pu := range p {
				if uInfo, err := s.RTM.GetUserInfo(strings.ToUpper(pu.Key)); err == nil {
					p[i] = Pair{
						Key:   uInfo.Name,
						Value: pu.Value,
					}
				} else {
					log.Printf("Eorror: %v\n", err)
				}
			}
			w, err := json.Marshal(response)
			if err == nil {
				rw.Write(w)
				return
			}
		}
		rw.Write([]byte("Evil Bot Says No!"))
	})

	s.RegisterEndpoint("/bottom5/{channelid}", "get", func(rw http.ResponseWriter, r *http.Request, br *evilbot.Response) {
		ch := mux.Vars(r)["channelid"]
		if len(ch) < 0 {
			ch = "all"
		}
		if ch != "all" {
			chInfo, err := br.ChannelInfo(ch)
			if err == nil {
				ch = chInfo.ID
			} else {
				rw.Write([]byte("Evil Bot Says No!"))
				return
			}
		}
		if p, err := a.BottomX(ch, 5); err == nil {
			var cname string
			if ch != "all" {
				if cInfo, err := s.RTM.GetChannelInfo(ch); err == nil {
					cname = cInfo.Name
				} else {
					rw.Write([]byte("Evil Bot Says No!"))
					return
				}
			} else {
				cname = "all activity"
			}
			response := map[string]interface{}{
				"channel": cname,
				"results": p,
			}
			for i, pu := range p {
				if uInfo, err := s.RTM.GetUserInfo(strings.ToUpper(pu.Key)); err == nil {
					p[i] = Pair{
						Key:   uInfo.Name,
						Value: pu.Value,
					}
				} else {
					log.Printf("Eorror: %v\n", err)
				}
			}
			w, err := json.Marshal(response)
			if err == nil {
				rw.Write(w)
				return
			}
		}
		rw.Write([]byte("Evil Bot!"))
	})
	return nil
}
Пример #2
0
func main() {
	defer log.Println("Exiting...")

	//Get the Slack API key from your environment variable
	log.Println("Provisioning bot...")
	api_key := os.Getenv("SLACKBOT")
	if len(api_key) < 1 {
		log.Printf("Please et proper SLACKBOT environment variable")
		return
	}

	//Load storage given slackbot key
	log.Printf("Loading storage volumes...")
	db, err := storage.Load(api_key)
	if err != nil {
		panic(err)
	}
	log.Printf("DB Loaded: %v\n", db)
	//Start a New Bot given the API Key and a command indntifier
	//Command identifier cannot be longer than 3 charachters
	b, err := evilbot.New(api_key, "!")
	if err != nil {
		log.Println("Not Authenticated, please check your ENV Setting")
		return
	}

	//Add Command Handlers, you can also create an array of handlers and loop trhough them
	b.AddCmdHandler(
		"test",
		TestCmdHandler,
	)
	b.AddCmdHandler(
		"help",
		TestCmdHandler2,
	)
	b.AddCmdHandler(
		"speak",
		SpeakCmdHandler,
	)
	//Adding general purpose event handlers. These can be used for anything other than a command
	//example would be keeping track of when a person was last active
	b.AddEventHandler(
		"event",
		TestGeneralHandler,
	)

	if err := db.LoadBucket("main"); err != nil {
		log.Panic(err)
	}

	if err := db.SetVal("main", "test2", "Test Value22"); err != nil {
		log.Panic(err)
	}

	if t, err := db.GetVal("main", "test2"); err == nil {
		log.Printf("Test Key: %v\n", t)
	} else {
		log.Printf("Reading Error: %v\n", err)
	}

	if t, err := db.GetVal("main", "test"); err == nil {
		log.Printf("Test Key: %v\n", t)
	} else {
		log.Printf("Reading Error: %v\n", err)
	}
	//Turn on logging to see ALL Incoming data
	b.SetLogging(true)

	//Register Additional Endpoints
	if err := b.RegisterEndpoint("/test/{channel}", "get", TestHTTPHandler); err != nil {
		log.Printf("Endpoint Error: %v\n", err)
	}

	if err := activitylog.NewLogger(b); err != nil {
		log.Panic(err)
	}

	//Run The Bot
	b.RunWithHTTP("8000")
}