Exemplo n.º 1
0
func main() {
	bot := bb.LoadBot(conf.GetItem("botapi"))
	if conf.GetItem("runMode") == "debug" {
		bot.SetUpdate(10)
	} else {
		bot.SetWebhook("jqs7.com", "8443",
			"crt.pem", "key.pem")
	}
	if bot.Err != nil {
		log.Println("bot connection failed")
		log.Println(bot.Err)
		return
	}

	go plugin.InitRss(bot.GetBot())
	go func() {
		mongo.MIndex()
		mongo.DailySave()
		scheduler.Every().Day().At("00:05").Run(mongo.DailySave)
	}()
	go webServer.GinServer()

	botName := bot.GetBot().Self.UserName
	bot.Prepare(&plugin.Prepare{}).
		Plugin(new(plugin.Start), "/help", "/start", "/help@"+botName, "/start@"+botName).
		Plugin(new(plugin.Rule), "/rule", "/rule@"+botName).
		Plugin(new(plugin.SetRule), "/setrule", "/setrule@"+botName).
		Plugin(new(plugin.RmRule), "/rmrule", "/rmrule@"+botName).
		Plugin(new(plugin.AutoRule), "/autorule", "/autorule@"+botName).
		Plugin(new(plugin.About), "/about", "/about@"+botName).
		Plugin(new(plugin.OtherResources), "/other_resources", "/other_resources@"+botName).
		Plugin(new(plugin.Subscribe), "/subscribe", "/subscribe@"+botName).
		Plugin(new(plugin.UnSubscribe), "/unsubscribe", "/unsubscribe@"+botName).
		Plugin(new(plugin.Broadcast), "/broadcast").
		Plugin(new(plugin.Groups), "/groups", "/groups@"+botName).
		Plugin(new(plugin.Cancel), "/cancel").
		Plugin(new(plugin.Base64), "/e64", "/d64").
		Plugin(new(plugin.Google), "/gg").
		Plugin(new(plugin.Trans), "/trans").
		Plugin(new(plugin.Man), "/man", "/setman", "/rmman").
		Plugin(new(plugin.Reload), "/reload").
		Plugin(new(plugin.Stat), "/os", "/df", "/free", "/redis").
		Plugin(new(plugin.Rain), "/rain").
		Plugin(new(plugin.Rss), "/rss", "/rmrss").
		Plugin(new(plugin.Markdown), "/md").
		Plugin(new(plugin.Search), "/search").
		Plugin(new(plugin.Turing), "@"+botName).
		Default(&plugin.Default{}).
		Start()
}
Exemplo n.º 2
0
func (d *Default) isMaster() bool {
	master := conf.GetItem("master")
	if d.Message.From.UserName == master {
		return true
	}
	return false
}
Exemplo n.º 3
0
func MSession() *mgo.Session {
	if mc == nil {
		var err error
		mgoURL := conf.GetItem("mgoUrl")
		mc, err = mgo.Dial(mgoURL)
		if err != nil {
			log.Println(err)
		}
	}
	return mc.Clone()
}
Exemplo n.º 4
0
func (t *Default) turing(text string) {
	msgText := make(chan string)
	chatAction := make(chan bool)
	asGroupMsg := false
	go func() {
		var userid string
		if (t.FromGroup || t.FromSuperGroup) && strings.HasPrefix(text, "-") {
			text = strings.TrimPrefix(text, "-")
			asGroupMsg = true
			userid = strconv.Itoa(t.ChatID)
		} else {
			userid = strconv.Itoa(t.Message.From.ID)
		}
		//语言检测,如果不是中文,则使用翻译后的结果
		reZh := regexp.MustCompile(`[\p{Han}]`).
			FindAllString(text, -1)
		if float32(len(reZh))/float32(utf8.RuneCountInString(text)) < 0.4 {
			m := &MsTrans{}
			m.New()
			from, _ := m.Detect(text)
			switch from {
			case "zh-CHS", "zh-CHT":
			default:
				text, _ = m.Trans(text, from, "zh-CHS")
			}
		}
		msgText <- TuringBot(conf.GetItem("turingBotKey"), userid, text)
	}()

	go func(done chan bool) {
		t.NewChatAction(t.ChatID).Typing().Send()
		done <- true
	}(chatAction)

	<-chatAction
	result := <-msgText
	if asGroupMsg {
		result = fmt.Sprintf("- %s", result)
	}

	t.NewMessage(t.ChatID, result).
		DisableWebPagePreview().Send()
	return
}
Exemplo n.º 5
0
func (m *MsTrans) New() {
	m.t = microsoft.NewTranslator(
		conf.GetItem("msTransId"),
		conf.GetItem("msTransSecret"))
}
Exemplo n.º 6
0
func GinServer() {
	r := gin.Default()
	if conf.GetItem("runMode") == "debug" {
		gin.SetMode(gin.DebugMode)
	} else {
		gin.SetMode(gin.ReleaseMode)
	}

	render := render.New(render.Options{
		Directory:  "html",
		Delims:     render.Delims{"<%", "%>"},
		Extensions: []string{".html"},
	})

	r.Static("/dist", "./html/dist")

	r.GET("/", func(c *gin.Context) {
		render.HTML(c.Writer, http.StatusOK, "index", nil)
	})

	r.GET("/api", func(c *gin.Context) {
		var total, users []interface{}
		limit := time.Now().AddDate(0, 0, -100)
		mongo.M("dailyTotal", func(c *mgo.Collection) {
			c.Find(bson.M{
				"date": bson.M{"$gt": limit},
			}).Sort("date").All(&total)
		})
		mongo.M("dailyUsersCount", func(c *mgo.Collection) {
			c.Find(bson.M{
				"date": bson.M{"$gt": limit},
			}).Sort("date").All(&users)
		})
		render.JSON(c.Writer, http.StatusOK,
			gin.H{"total": total, "users": users})
	})

	r.GET("/api/rank/:date", func(c *gin.Context) {
		s := c.Params.ByName("date")
		loc, _ := time.LoadLocation("Asia/Shanghai")
		date, err := time.ParseInLocation("2006-01-02", s, loc)
		if err != nil {
			return
		}
		var result interface{}
		mongo.M("dailyRank", func(c *mgo.Collection) {
			c.Find(
				bson.M{"date": date},
			).One(&result)
		})
		render.JSON(c.Writer, http.StatusOK, result)
	})

	r.GET("/api/user/:name", func(c *gin.Context) {
		limit := time.Now().AddDate(0, 0, -100)
		s, err := url.QueryUnescape(c.Params.ByName("name"))
		if err != nil {
			return
		}
		var result []interface{}
		userid := conf.Redis.HGet("tgUsersName", s).Val()
		mongo.M("dailyUser", func(c *mgo.Collection) {
			c.Find(
				bson.M{
					"user": userid,
					"date": bson.M{"$gt": limit},
				},
			).Sort("date").All(&result)
		})
		render.JSON(c.Writer, http.StatusOK,
			gin.H{"result": result, "userName": s})
	})

	r.GET("/user/:name", func(c *gin.Context) {
		render.HTML(c.Writer, http.StatusOK, "user", nil)
	})

	ginpprof.Wrapper(r)
	r.Run(":6060")
}