Пример #1
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)
		}
	}
}
Пример #2
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)
	}
}