Exemplo n.º 1
0
func (this *Chinese) Default(name string, msg telegram.TObject, state *map[string]interface{}) {
	if msg["text"] != nil {
		text := msg["text"].(string)
		if !strings.HasPrefix(text, "/") {
			this.Learn(text, msg.ChatId())
		}
	}
}
Exemplo n.º 2
0
func Gun(tg *telegram.Telegram, msg telegram.TObject) bool {
	if msg["text"] != nil {
		out := eng.Calculate(common.TextToSample(msg["text"].(string)))
		log.Println(out)

		if (out[0] > 0.5) && (out[0]-out[1] > 0.3) {
			tg.ReplyToMessage(msg.MessageId(), "#RICH", msg.ChatId())
			return true
		}
	}

	return false
}
Exemplo n.º 3
0
func (this *Help) Command(name string, msg telegram.TObject, args []string) {
	if name == "help" {
		str := ""
		for _, v := range *this.cmds {
			// Skip debug functions
			if v.Debug {
				continue
			}

			str += fmt.Sprintf(
				"/%s %s\n%s\n\n",
				v.Name, v.Args, v.Desc)
		}
		this.tg.ReplyToMessage(msg.MessageId(), str, msg.ChatId())
	}
}
Exemplo n.º 4
0
func (this *Scholar) Default(name string, msg telegram.TObject, state *map[string]interface{}) {
	if name == "google" {
		if (msg["text"] == nil) || (strings.ToLower(msg["text"].(string)) != "next") {
			return
		}

		start := (*state)["start"].(int)
		query := (*state)["query"].(string)

		this.tg.SendChatAction("typing", msg.ChatId())
		res, hasNext := Google(query, start, 5, this.ipv6)

		this.tg.SendMessageNoPreview(formatGoogle(res, hasNext), msg.ChatId())

		if hasNext {
			(*state)["start"] = start + len(res)
		} else {
			utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
		}
	}
}
Exemplo n.º 5
0
func handle(msg telegram.TObject) {
	if Debug {
		log.Println(msg)
	}

	// Parse arguments (including the command)
	args := make([]string, 0)
	text := ""

	if msg["text"] != nil {
		text = strings.Trim(msg["text"].(string), " ")
	}

	if text != "" {
		args = telegram.ParseArgs(text)
	}

	// A command
	if (len(args) > 0) && strings.HasPrefix(args[0], "/") {
		cmd := args[0][1:]
		args = args[1:]

		if strings.Contains(cmd, "@") {
			if !strings.HasSuffix(cmd, "@"+BotName) {
				// This command is not our business
				return
			} else {
				cmd = cmd[0:strings.Index(cmd, "@")]
			}
		}

		command := Commands[cmd]

		if command.Processor == nil {
			return
		}

		if (command.ArgNum < 0) || (command.ArgNum == len(args)) {
			command.Processor.Command(cmd, msg, args)
		} else {
			str := fmt.Sprintf("Usage: /%s %s\nDescription: %s", command.Name, command.Args, command.Desc)
			Telegram.ReplyToMessage(
				msg.MessageId(),
				str,
				msg.ChatId())
		}
	} else {
		if utils.HasGrabber(msg.FromId(), msg.ChatId()) {
			// Distribute to grabbers
			name, processor := utils.Grabber(msg.FromId(), msg.ChatId())
			processor.Default(name, msg, utils.GrabberState(msg.FromId(), msg.ChatId()))
		}

		// Grabbers and default processors are both called
		if Default.Processor != nil {
			// Distribute to default processor
			Default.Processor.Default(Default.Name, msg, nil)
		}
	}
}
Exemplo n.º 6
0
func (this *Misc) Default(name string, msg telegram.TObject, state *map[string]interface{}) {
	if name == "remind" {
		if (*state)["remind"] == nil {
			(*state)["remind"] = msg["text"].(string)
			this.tg.ReplyToMessage(msg.MessageId(), "How long after now should I remind you?", msg.ChatId())
		} else {
			duration, err := time.ParseDuration(msg["text"].(string))
			if err != nil {
				this.tg.ReplyToMessage(msg.MessageId(), "Invalid time. Supported format: BhCmDsEmsFnsGus", msg.ChatId())
			} else {
				text := (*state)["remind"].(string)

				this.tg.ReplyToMessage(msg.MessageId(), "Yes, sir!", msg.ChatId())

				utils.PostDelayed(func() {
					this.tg.SendMessage("@"+msg.From()["username"].(string)+" "+text, msg.ChatId())
				}, duration)

				utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
			}
		}
	}
}
Exemplo n.º 7
0
func (this *Misc) Command(name string, msg telegram.TObject, args []string) {
	switch name {
	case "echo":
		this.tg.SendMessage(args[0], msg.ChatId())
	case "parse":
		this.tg.ReplyToMessage(msg.MessageId(), strings.Join(args, "\n"), msg.ChatId())
	case "cancel":
		if utils.HasGrabber(msg.FromId(), msg.ChatId()) {
			utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
			this.tg.SendMessage("Current session cancelled", msg.ChatId())
		}
	case "remind":
		this.tg.ReplyToMessage(msg.MessageId(), "What do you want me to remind you of?", msg.ChatId())
		utils.SetGrabber(types.Grabber{
			Name:      "remind",
			Uid:       msg.FromId(),
			Chat:      msg.ChatId(),
			Processor: this,
		})
	case "choose":
		this.choose(msg, args)
	}
}
Exemplo n.º 8
0
func (this *Misc) choose(msg telegram.TObject, args []string) {
	if len(args) <= 1 {
		this.tg.ReplyToMessage(msg.MessageId(), "Please provide the output format.", msg.ChatId())
	} else {
		items := strings.Split(args[0], ";")
		results := make([]interface{}, len(items))
		for i, v := range items {
			// Random number in a range
			if strings.HasPrefix(v, "[") && strings.HasSuffix(v, "]") && strings.Contains(v, "-") {
				v = v[1 : len(v)-1]
				a := strings.Split(v, "-")
				if len(a) == 2 {
					start, err1 := strconv.ParseInt(a[0], 10, 64)
					end, err2 := strconv.ParseInt(a[1], 10, 64)

					if (err1 == nil) && (err2 == nil) {
						r := float64(start) + rand.Float64()*float64(end-start)
						results[i] = r
						continue
					}
				}

				this.tg.ReplyToMessage(msg.MessageId(), "Range format: [start-end]", msg.ChatId())
				return
			} else {
				a := strings.Split(v, ",")
				results[i] = a[rand.Intn(len(a))]
			}
		}

		format := strings.Join(args[1:], " ")
		tokens := ParseFormat(format)

		// Now let's do the heavy type conversion stuff
		i := 0
		for _, t := range tokens {
			if i >= len(results) {
				break
			}

			if len(t) < 1 {
				continue
			}

			switch t[len(t)-1] {
			case 'b', 'c', 'd', 'o', 'q', 'x', 'X', 'U':
				// Integer
				switch t := results[i].(type) {
				case string:
					results[i], _ = strconv.ParseInt(results[i].(string), 10, 64)
				case float64:
					results[i] = int64(results[i].(float64))
				default:
					_ = t
				}
			case 'e', 'E', 'f', 'F', 'g', 'G':
				// Float
				switch t := results[i].(type) {
				case string:
					results[i], _ = strconv.ParseFloat(results[i].(string), 64)
				case int64:
					results[i] = float64(results[i].(int64))
				default:
					_ = t
				}
			}

			if t != "%" {
				i += 1
			}
		}

		this.tg.ReplyToMessage(
			msg.MessageId(),
			fmt.Sprintf(format, results...),
			msg.ChatId())
	}
}
Exemplo n.º 9
0
func (this *Chinese) Command(name string, msg telegram.TObject, args []string) {
	if name == "learn" {
		this.Learn(strings.Join(args, " "), msg.ChatId())
	} else if name == "speak" {
		this.tg.SendMessage(this.Speak(msg.ChatId()), msg.ChatId())
	} else if name == "answer" {
		text := strings.Join(args, " ")
		id := msg.MessageId()

		if (text == "") && (msg["reply_to_message"] != nil) && (msg.ReplyToMessage()["text"] != nil) {
			text = msg.ReplyToMessage()["text"].(string)
			id = msg.ReplyToMessage().MessageId()
		}

		text = strings.Trim(text, " \n")

		if text == "" {
			this.tg.ReplyToMessage(msg.MessageId(), "Please provide a question or reply to a message for me to answer.", msg.ChatId())
		} else {
			r := this.Answer(text, msg.ChatId())

			if r != "" {
				this.tg.ReplyToMessage(id, r, msg.ChatId())
			}
		}
	}
}
Exemplo n.º 10
0
func (this *Help) Command(name string, msg telegram.TObject, args []string) {
	if name == "help" {
		if !msg.Chat().IsGroup() {
			str := "Source code available at https://github.com/PeterCxy/gotgbot , written in Golang\n\n"
			for _, v := range *this.cmds {
				// Skip debug functions
				if v.Debug {
					continue
				}

				str += fmt.Sprintf(
					"/%s %s\n%s\n\n",
					v.Name, v.Args, v.Desc)
			}
			this.tg.ReplyToMessage(msg.MessageId(), str, msg.ChatId())
		} else {
			this.tg.ReplyToMessage(msg.MessageId(), "Help only available in private chats.", msg.ChatId())
		}
	} else if name == "father" {
		if !msg.Chat().IsGroup() {
			str := ""
			for _, v := range *this.cmds {
				if v.Debug {
					continue
				}

				str += fmt.Sprintf(
					"%s - %s %s\n",
					v.Name, v.Args, strings.Split(v.Desc, "\n")[0])
			}
			this.tg.ReplyToMessage(msg.MessageId(), str, msg.ChatId())
		}
	}
}
Exemplo n.º 11
0
func (this *Scholar) Command(name string, msg telegram.TObject, args []string) {
	if name == "calc" {
		res, err := calc.Calculate(strings.Join(args, " "))

		if err == nil {
			this.tg.ReplyToMessage(msg.MessageId(), fmt.Sprintf("%f", res), msg.ChatId())
		} else {
			this.tg.ReplyToMessage(msg.MessageId(), err.Error(), msg.ChatId())
		}
	} else if name == "google" {
		query := strings.Join(args, " ")

		if query == "" {
			this.tg.ReplyToMessage(msg.MessageId(), "Please provide something to search for.", msg.ChatId())
		} else {
			num := 5
			irc := false

			if (msg.Chat()["title"] != nil) && strings.HasPrefix(msg.Chat()["title"].(string), "#") {
				num = 1 // Disable long output in IRC-connected groups
				irc = true
			}

			this.tg.SendChatAction("typing", msg.ChatId())
			res, hasNext := Google(query, 0, num, this.ipv6)

			if irc {
				hasNext = false
			}

			this.tg.SendMessageNoPreview(formatGoogle(res, hasNext), msg.ChatId())

			if hasNext {
				state := utils.SetGrabber(types.Grabber{
					Name:      "google",
					Uid:       msg.FromId(),
					Chat:      msg.ChatId(),
					Processor: this,
				})

				(*state)["start"] = len(res)
				(*state)["query"] = query
			}
		}
	}
}