Example #1
0
func (g gmService) PostText(key, text string, cacheID int, groupMessage service.Message) {

	dividedText := utility.DivideString(text, groupmeLengthLimit)
	if len(dividedText) > 2 {
		pasteBinURL := postToPastebin(text)
		if len(pasteBinURL) == 0 {
			return
		}

		dividedText = []string{pasteBinURL}
	}

	for _, subText := range dividedText {
		go func(key, message string) {
			postBody := map[string]string{
				"bot_id": key,
				"text":   message,
			}

			encoded, err := json.Marshal(postBody)
			if err != nil {
				return
			}

			postToGroupMe(encoded)
			mID, err := messageID(groupMessage)
			if err == nil {
				cachePost(cacheID, mID, groupMessage.GroupID())
			}
		}(key, subText)
	}

	go updateLikes()
}
Example #2
0
func messageID(message service.Message) (string, error) {
	time.Sleep(messagePollDelayMilliseconds * time.Millisecond)
	token := os.Getenv("groupMeID")
	getURL := "https://api.groupme.com/v3/groups/" + message.GroupID() + "/messages?token=" + token + "&after_id=" + message.MessageID()
	resp, _ := http.Get(getURL)

	body, _ := ioutil.ReadAll(resp.Body)

	var wrapper gmMessageWrapper
	json.Unmarshal(body, &wrapper)

	for _, recieved := range wrapper.Response.Messages {
		if recieved.UserType() == "bot" {
			return recieved.MessageID(), nil
		}
	}

	return "", errors.New("No messages found")
}
Example #3
0
func responseForMessage(message service.Message, bot models.Bot) (string, models.Action) {
	actions, _ := models.FetchActions(true)
	sort.Sort(models.ByPriority(actions))

	var (
		act    models.Action
		sMatch string
	)
	for _, a := range actions {
		regexString := strings.Replace(*a.Pattern, "{_botname_}", bot.BotName, -1)
		r, _ := regexp.Compile("(?i)" + regexString)
		matched := r.FindStringSubmatch(message.Text())
		if len(matched) > 1 && matched[1] != "" {
			sMatch = matched[1]
			act = a
			break
		}
	}

	var (
		postString string
		err        error
	)
	for {
		updateAction(&act, sMatch)
		if act.IsURLType() {
			postString, err = handleURLAction(act, bot)
		} else {
			postString = act.Content
		}

		if (err == nil && postString != "") || act.FallbackAction == nil {
			break
		} else {
			act, _ = models.FetchAction(*act.FallbackAction)
		}
	}

	return postString, act
}
Example #4
0
func HandleCalman(message service.Message, service service.Service, cache cache.QueryCache) {

	if message.UserType() != "user" {
		return
	}

	bot, _ := models.FetchBot(message.GroupID())

	// Make sure the message has the bot's name with a preceeding character, and that it isn't escaped
	index := strings.Index(strings.ToLower(message.Text()), strings.ToLower(bot.BotName))
	isEscaped := (index >= 2 && message.Text()[index-2] == '\\')
	if len(message.Text()) < 1 || index < 1 || isEscaped {
		return
	}

	var (
		postString string
		act        models.Action
	)

	if cached := cache.CachedResponse(message.Text()); cached != nil {
		postString = *cached
	} else {
		postString, act = responseForMessage(message, bot)
	}

	postString = updatedPostText(act, postString)
	postString = utility.ProcessedString(postString)

	cacheID := cache.CacheQuery(message.Text(), postString)

	fmt.Printf("Query: %v\n", message.Text())
	if postString != "" {
		fmt.Printf("Action: %v\n", act.Content)
		fmt.Printf("Posting: %v\n", postString)
		service.PostText(bot.Key, postString, cacheID, message)
	}
}