Пример #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()
}
Пример #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")
}
Пример #3
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)
	}
}