예제 #1
0
파일: misc.go 프로젝트: PeterCxy/gotgbot
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() {
					if !strings.HasPrefix(text, "\\/") {
						this.tg.SendMessage("@"+msg.From()["username"].(string)+" "+text, msg.ChatId())
					} else {
						msg["text"] = text[1:]
						utils.Handler()(msg)
					}
				}, duration)

				utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
			}
		}
	}
}
예제 #2
0
파일: scholar.go 프로젝트: PeterCxy/gotgbot
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
			maxNum := 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, maxNum, this.ipv6)

			if len(res) > num {
				res = res[0:num]
			}

			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
			}
		}
	}
}
예제 #3
0
파일: scholar.go 프로젝트: wan-qy/gotgbot
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())
		}
	}
}
예제 #4
0
파일: server.go 프로젝트: PeterCxy/gotgbot
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)
		}
	}
}
예제 #5
0
파일: misc.go 프로젝트: wan-qy/gotgbot
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)
	}
}