Esempio n. 1
1
// Loop is an infinite loop that checks for new Ranssi posts
func Loop(bot *telebot.Bot, noNotifAtInit bool) {
	for {
		readNow := config.GetConfig().LastReadPost + 1

		node := getPost(readNow)
		if node != nil {
			topic := strings.TrimSpace(node.FirstChild.FirstChild.Data)

			log.Infof("[Posts] New post detected: %s (ID %d)", topic, readNow)

			if !noNotifAtInit {
				config.GetUsersWithSettingAndRun(func(u config.User) {
					bot.SendMessage(u, lang.Translatef(u, "posts.new", topic, readNow), util.Markdown)
				}, subSetting)
			}

			config.GetConfig().LastReadPost = readNow
			config.ASave()
			updateNews()
			time.Sleep(5 * time.Second)
			continue
		}
		noNotifAtInit = false
		time.Sleep(1 * time.Minute)
	}
}
Esempio n. 2
0
func sendSecondYear(day int, bot *telebot.Bot, message telebot.Message) {
	sender := config.GetUserWithUID(message.Sender.ID)
	bot.SendMessage(message.Chat,
		lang.Translatef(sender, "timetable.generic",
			secondyear[day][0].Subject, secondyear[day][1].Subject, secondyear[day][2].Subject, secondyear[day][3].Subject,
			util.DateToString(secondyear[day][0].Date))+"\n"+lang.Translatef(sender, "timetable.other", other[day].Subject),
		util.Markdown)
}
Esempio n. 3
0
// HandleCommand handles a /timetable command
func HandleCommand(bot *telebot.Bot, message telebot.Message, args []string) {
	sender := config.GetUserWithUID(message.Sender.ID)
	if util.Timestamp() > lastupdate+600 {
		bot.SendMessage(message.Chat, lang.Translatef(sender, "timetable.update"), util.Markdown)
		Update()
	}

	day := today
	year := sender.Year
	if len(args) == 1 {
		if util.CheckArgs(args[0], lang.Translate(sender, "timetable.year.first")) {
			year = 1
		} else if util.CheckArgs(args[0], lang.Translate(sender, "timetable.year.second")) {
			year = 2
		} else if util.CheckArgs(args[0], "update") {
			Update()
			bot.SendMessage(message.Chat, lang.Translate(sender, "timetable.update.success"), util.Markdown)
		} else {
			dayNew, err := shift(day, args[0], 0, len(other))
			if err != nil {
				if err.Error() == "OOB" {
					bot.SendMessage(message.Chat, lang.Translate(sender, "timetable.nodata"), util.Markdown)
				} else if err.Error() == "PARSEINT" {
					bot.SendMessage(message.Chat, lang.Translate(sender, "timetable.usage"), util.Markdown)
				}
				return
			}
			day = dayNew
		}
	} else if len(args) == 2 {
		if util.CheckArgs(args[0], lang.Translate(sender, "timetable.year.first")) {
			year = 1
		} else if util.CheckArgs(args[0], lang.Translate(sender, "timetable.year.second")) {
			year = 2
		} else {
			bot.SendMessage(message.Chat, lang.Translate(sender, "timetable.usage"), util.Markdown)
		}
		dayNew, err := shift(day, args[1], 0, len(other))
		if err != nil {
			if err.Error() == "OOB" {
				bot.SendMessage(message.Chat, lang.Translate(sender, "timetable.nodata"), util.Markdown)
			} else if err.Error() == "PARSEINT" {
				bot.SendMessage(message.Chat, lang.Translate(sender, "timetable.usage"), util.Markdown)
			}
			return
		}
		day = dayNew
	}

	if day < 0 || day >= len(other) {
		bot.SendMessage(message.Chat, lang.Translate(sender, "timetable.nodata"), util.Markdown)
		return
	}

	if year == 1 {
		sendFirstYear(day, bot, message)
	} else if year == 2 {
		sendSecondYear(day, bot, message)
	} else {
		bot.SendMessage(message.Chat, lang.Translate(sender, "timetable.noyeargroup"), util.Markdown)
	}
}