Esempio n. 1
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
			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
			}
		}
	}
}
Esempio n. 2
0
func (this *Barcode) Command(name string, msg telegram.TObject, args []string) {
	if name == "barcode" {
		if msg["reply_to_message"] != nil {
			// Decode the message replied to
			this.Decode(msg.ReplyToMessage())
		} else {
			// Decode from grabbed input
			this.tg.ReplyToMessage(msg.MessageId(), "Now send me the picture to decode.", msg.ChatId())
			utils.SetGrabber(types.Grabber{
				Name:      "barcode",
				Uid:       msg.FromId(),
				Chat:      msg.ChatId(),
				Processor: this,
			})
		}
	}
}
Esempio n. 3
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)
	}
}
Esempio n. 4
0
func (this *Script) Command(name string, msg telegram.TObject, args []string) {
	if name == "code" {
		this.tg.ReplyToMessage(msg.MessageId(), "Now send me some code. You can split it into multiple messages.\nAfter finishing, send 'exec script_type' to execute it as a script.", msg.ChatId())
		status := utils.SetGrabber(types.Grabber{
			Name:      "code",
			Uid:       msg.FromId(),
			Chat:      msg.ChatId(),
			Processor: this,
		})

		(*status)["code"] = ""
	} else if name == "brainfuck" {
		code := strings.Trim(args[0], " \n")

		if code == "" {
			this.tg.ReplyToMessage(msg.MessageId(), "Code is empty.", msg.ChatId())
		} else {
			end := time.Now().Add(time.Duration(int64(this.timeout)) * time.Second)
			res, err := brainfuck.New().SetInterrupter(func() bool {
				return time.Now().After(end)
			}).SetInput(func(out string) string {
				this.tg.ReplyToMessage(msg.MessageId(),
					fmt.Sprintf("Output: %s\nInput needed. Now send me the input data in 60 seconds. If nothing received, the interpreter will be interrupted.\nSend /cancel to force interrupt.", out),
					msg.ChatId())

				status := utils.SetGrabber(types.Grabber{
					Name:      "brainfuck",
					Uid:       msg.FromId(),
					Chat:      msg.ChatId(),
					Processor: this,
				})

				input := make(chan string)
				(*status)["chan"] = input

				now := time.Now()
				var dur time.Duration
				if now.Before(end) {
					dur = end.Sub(now)
				} else {
					// WTF??
					dur = time.Duration(0)
				}
				select {
				case result := <-input:
					end = time.Now().Add(dur)
					return result
				case <-time.After(60 * time.Second):
					utils.ReleaseGrabber(msg.FromId(), msg.ChatId())
					this.tg.ReplyToMessage(msg.MessageId(), "Input timed out. Interrupting.", msg.ChatId())
					return string(0)
				}

				return string(0)
			}).Exec(code)

			if err != nil {
				this.tg.ReplyToMessage(msg.MessageId(), err.Error(), msg.ChatId())
			} else {
				res = strings.Trim(res, " \n")

				if res == "" {
					res = "Empty"
				}

				this.tg.ReplyToMessage(msg.MessageId(), res, msg.ChatId())
			}
		}
	}
}