<commit_before>package main

import (
	"fmt"
	"github.com/evolsnow/robot/conn"
	"github.com/go-telegram-bot-api/telegram-bot-api"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"os"
	"os/exec"
	"regexp"
	"strings"
	"sync"
	"time"
)

const (
	FirstFormat  = "1/02 15:04"
	SecondFormat = "15:04"
	ThirdFormat  = "15:04:05"
	RedisFormat  = "1/02 15:04:05" //save to redis format
)

//zmz.tv needs to login before downloading
var zmzClient http.Client

func (rb *Robot) Start(update tgbotapi.Update) string {
	user := update.Message.Chat.UserName
	go conn.SetUserChatId(user, update.Message.Chat.ID)
	return "welcome: " + user
}

func (rb *Robot) Help(update tgbotapi.Update) string {
	helpMsg := `
/alarm - set a reminder
/allalarms -show all of your alarms
/allmemos - show all of your memos
/evolve	- self evolution of me
/memo  save a memo
/movie - find movie download links
/show - find American show download links
/trans - translate words between english and chinese
/help - show this help message
`
	return helpMsg
}

func (rb *Robot) Evolve(update tgbotapi.Update) {
	if update.Message.Chat.FirstName != "Evol" || update.Message.Chat.LastName != "Gan" {
		rb.bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "sorry, unauthorized"))
		return
	}
	<-saidGoodBye
	close(saidGoodBye)
	cmd := exec.Command("bash", "/root/evolve_"+rb.nickName)
	cmd.Start()
	os.Exit(1)
}

func (rb *Robot) Translate(update tgbotapi.Update) string {
	var info string
	if string(update.Message.Text[0]) == "/" {
		raw := strings.SplitAfterN(update.Message.Text, " ", 2)
		if len(raw) < 2 {
			return "what do you want to translate, try '/trans cat'?"
		} else {
			info = "翻译" + raw[1]
		}
	} else {
		info = update.Message.Text
	}

	return qinAI(info)

}
func (rb *Robot) Talk(update tgbotapi.Update) string {
	info := update.Message.Text
	if strings.Contains(info, rb.name) {
		if strings.Contains(info, "闭嘴") || strings.Contains(info, "别说话") {
			rb.shutUp = true
		} else if rb.shutUp && strings.Contains(info, "说话") {
			rb.shutUp = false
			return fmt.Sprintf("%s终于可以说话啦", rb.nickName)
		}
		info = strings.Replace(info, fmt.Sprintf("@%s", rb.name), "", -1)
	}

	if rb.shutUp {
		return ""
	}
	log.Printf(info)

	if rb.nickName == "samaritan" {
		if chinese(info) {
			return tlAI(info)
		} else {
			return mitAI(info)
		}
	} else { //jarvis use another AI
		return qinAI(info)
	}
	//	return response
}

func (rb *Robot) SetReminder(update tgbotapi.Update, step int) string {
	user := update.Message.Chat.UserName
	switch step {
	case 0:
		//known issue of go, you can not just assign update.Message.Chat.ID to userTask[user].ChatId
		tmpTask := userTask[user]
		tmpTask.ChatId = update.Message.Chat.ID
		tmpTask.Owner = user
		userTask[user] = tmpTask

		tmpAction := userAction[user]
		tmpAction.ActionStep++
		userAction[user] = tmpAction
		return "Ok, what should I remind you to do?"
	case 1:
		//save thing
		tmpTask := userTask[user]
		tmpTask.Desc = update.Message.Text
		userTask[user] = tmpTask

		tmpAction := userAction[user]
		tmpAction.ActionStep++
		userAction[user] = tmpAction
		return "When or how much time after?\n" +
			"You can type:\n" +
			"'*2/14 11:30*' means 11:30 at 2/14 \n" + //first format
			"'*11:30*' means  11:30 today\n" + //second format
			"'*5m10s*' means 5 minutes 10 seconds later" //third format
	case 2:
		defer delete(userAction, user)
		//save time duration
		text := update.Message.Text
		text = strings.Replace(text, "：", ":", -1)
		var showTime string  //show to user
		var redisTime string //time string to save to redis
		var scheduledTime time.Time
		var nowTime = time.Now()
		var du time.Duration
		var err error
		if strings.Contains(text, ":") {
			scheduledTime, err = time.Parse(FirstFormat, text)
			//			nowTime, _ = time.Parse(FirstFormat, nowTime.Format(FirstFormat))
			showTime = scheduledTime.Format(FirstFormat)
			redisTime = scheduledTime.Format(RedisFormat)
			//second case
			if err != nil { //try to parse with second format
				scheduledTime, err = time.Parse(SecondFormat, text)
				redisTime = nowTime.Format("1/02 ") + scheduledTime.Format(ThirdFormat)
				//				nowTime, _ = time.Parse(SecondFormat, nowTime.Format(SecondFormat))
				showTime = scheduledTime.Format(SecondFormat)

				if err != nil {
					return "wrong format, try '2/14 11:30' or '11:30'?"
				}
			}
		} else { //third case

			du, err = time.ParseDuration(text)
			scheduledTime = nowTime.Add(du)
			showTime = scheduledTime.Format(ThirdFormat)
			redisTime = scheduledTime.Format(RedisFormat)

			if err != nil {
				return "wrong format, try '1h2m3s'?"
			}
		}
		//save time
		tmpTask := userTask[user]
		tmpTask.When = redisTime
		userTask[user] = tmpTask

		go rb.DoTask(userTask[user])
		return fmt.Sprintf("Ok, I will remind you that\n*%s* - *%s*", showTime, userTask[user].Desc)
	}
	return ""
}

func (rb *Robot) DoTask(ts conn.Task) {
	conn.HSetTask(*ts)
	nowString := time.Now().Format(RedisFormat)
	now, _ := time.Parse(RedisFormat, nowString)
	when, _ := time.Parse(RedisFormat, ts.When)
	if when.After(now) {<commit_msg>save task<commit_after>package main

import (
	"fmt"
	"github.com/evolsnow/robot/conn"
	"github.com/go-telegram-bot-api/telegram-bot-api"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"os"
	"os/exec"
	"regexp"
	"strings"
	"sync"
	"time"
)

const (
	FirstFormat  = "1/02 15:04"
	SecondFormat = "15:04"
	ThirdFormat  = "15:04:05"
	RedisFormat  = "1/02 15:04:05" //save to redis format
)

//zmz.tv needs to login before downloading
var zmzClient http.Client

func (rb *Robot) Start(update tgbotapi.Update) string {
	user := update.Message.Chat.UserName
	go conn.SetUserChatId(user, update.Message.Chat.ID)
	return "welcome: " + user
}

func (rb *Robot) Help(update tgbotapi.Update) string {
	helpMsg := `
/alarm - set a reminder
/allalarms -show all of your alarms
/allmemos - show all of your memos
/evolve	- self evolution of me
/memo  save a memo
/movie - find movie download links
/show - find American show download links
/trans - translate words between english and chinese
/help - show this help message
`
	return helpMsg
}

func (rb *Robot) Evolve(update tgbotapi.Update) {
	if update.Message.Chat.FirstName != "Evol" || update.Message.Chat.LastName != "Gan" {
		rb.bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "sorry, unauthorized"))
		return
	}
	<-saidGoodBye
	close(saidGoodBye)
	cmd := exec.Command("bash", "/root/evolve_"+rb.nickName)
	cmd.Start()
	os.Exit(1)
}

func (rb *Robot) Translate(update tgbotapi.Update) string {
	var info string
	if string(update.Message.Text[0]) == "/" {
		raw := strings.SplitAfterN(update.Message.Text, " ", 2)
		if len(raw) < 2 {
			return "what do you want to translate, try '/trans cat'?"
		} else {
			info = "翻译" + raw[1]
		}
	} else {
		info = update.Message.Text
	}

	return qinAI(info)

}
func (rb *Robot) Talk(update tgbotapi.Update) string {
	info := update.Message.Text
	if strings.Contains(info, rb.name) {
		if strings.Contains(info, "闭嘴") || strings.Contains(info, "别说话") {
			rb.shutUp = true
		} else if rb.shutUp && strings.Contains(info, "说话") {
			rb.shutUp = false
			return fmt.Sprintf("%s终于可以说话啦", rb.nickName)
		}
		info = strings.Replace(info, fmt.Sprintf("@%s", rb.name), "", -1)
	}

	if rb.shutUp {
		return ""
	}
	log.Printf(info)

	if rb.nickName == "samaritan" {
		if chinese(info) {
			return tlAI(info)
		} else {
			return mitAI(info)
		}
	} else { //jarvis use another AI
		return qinAI(info)
	}
	//	return response
}

func (rb *Robot) SetReminder(update tgbotapi.Update, step int) string {
	user := update.Message.Chat.UserName
	switch step {
	case 0:
		//known issue of go, you can not just assign update.Message.Chat.ID to userTask[user].ChatId
		tmpTask := userTask[user]
		tmpTask.ChatId = update.Message.Chat.ID
		tmpTask.Owner = user
		userTask[user] = tmpTask

		tmpAction := userAction[user]
		tmpAction.ActionStep++
		userAction[user] = tmpAction
		return "Ok, what should I remind you to do?"
	case 1:
		//save thing
		tmpTask := userTask[user]
		tmpTask.Desc = update.Message.Text
		userTask[user] = tmpTask

		tmpAction := userAction[user]
		tmpAction.ActionStep++
		userAction[user] = tmpAction
		return "When or how much time after?\n" +
			"You can type:\n" +
			"'*2/14 11:30*' means 11:30 at 2/14 \n" + //first format
			"'*11:30*' means  11:30 today\n" + //second format
			"'*5m10s*' means 5 minutes 10 seconds later" //third format
	case 2:
		defer delete(userAction, user)
		//save time duration
		text := update.Message.Text
		text = strings.Replace(text, "：", ":", -1)
		var showTime string  //show to user
		var redisTime string //time string to save to redis
		var scheduledTime time.Time
		var nowTime = time.Now()
		var du time.Duration
		var err error
		if strings.Contains(text, ":") {
			scheduledTime, err = time.Parse(FirstFormat, text)
			//			nowTime, _ = time.Parse(FirstFormat, nowTime.Format(FirstFormat))
			showTime = scheduledTime.Format(FirstFormat)
			redisTime = scheduledTime.Format(RedisFormat)
			//second case
			if err != nil { //try to parse with second format
				scheduledTime, err = time.Parse(SecondFormat, text)
				redisTime = nowTime.Format("1/02 ") + scheduledTime.Format(ThirdFormat)
				//				nowTime, _ = time.Parse(SecondFormat, nowTime.Format(SecondFormat))
				showTime = scheduledTime.Format(SecondFormat)

				if err != nil {
					return "wrong format, try '2/14 11:30' or '11:30'?"
				}
			}
		} else { //third case

			du, err = time.ParseDuration(text)
			scheduledTime = nowTime.Add(du)
			showTime = scheduledTime.Format(ThirdFormat)
			redisTime = scheduledTime.Format(RedisFormat)

			if err != nil {
				return "wrong format, try '1h2m3s'?"
			}
		}
		//save time
		tmpTask := userTask[user]
		tmpTask.When = redisTime
		userTask[user] = tmpTask

		go rb.DoTask(userTask[user])
		return fmt.Sprintf("Ok, I will remind you that\n*%s* - *%s*", showTime, userTask[user].Desc)
	}
	return ""
}

func (rb *Robot) DoTask(ts conn.Task) {
	conn.HSetTask(&ts)
	nowString := time.Now().Format(RedisFormat)
	now, _ := time.Parse(RedisFormat, nowString)
	when, _ := time.Parse(RedisFormat, ts.When)
	if when.After(now) {
{'fraction_non_alphanumeric': 0.14077860794337396, 'fraction_numerical': 0.015926071569012976, 'mean_word_length': 3.5907039711191335, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '387762', 'n_tokens_mistral': 4138, 'n_tokens_neox': 3745, 'n_words': 933}