Exemplo n.º 1
0
func listen(api *telegram.Api) {
	fmt.Println("Listen")
	//This code run async
	opt := make(map[string]string)
	opt["timeout"] = "120"

	var maxOffsetId int64
	maxOffsetId = 0
	for {
		fmt.Println("Pool")
		updates, err := api.GetUpdates(opt)
		if err != nil {
			fmt.Println(err)
			continue
		}
		fmt.Println(len(updates))
		for _, element := range updates {
			fmt.Printf("UpdateID = %s", element.UpdateId)
			fmt.Println(element.Message.Text)

			re := regexp.MustCompile("find (.+) in (.+)$")
			match := re.FindAllStringSubmatch(element.Message.Text, -1)
			if match != nil && len(match) >= 1 {
				findYelp(api, match[0][1], match[0][2])
				fmt.Println("FINDDD")
				continue
			}

			re = regexp.MustCompile("xin ko\\?")
			match = re.FindAllStringSubmatch(element.Message.Text, -1)
			if match != nil && len(match) >= 1 {
				api.SendMessage(map[string]string{
					"chat_id": "-28394494",
					"text":    "YES, I AM!!!",
				})
				continue
			}

			api.SendMessage(map[string]string{
				"chat_id": "-28394494",
				"text":    "DONT KNOW WHAT YOU ARE TALKING ABOUT. HERE IS A GOOGLE RESULT: ",
			})

			if element.UpdateId > maxOffsetId {
				maxOffsetId = element.UpdateId
			}
		}
		opt["offset"] = strconv.FormatInt(maxOffsetId+1, 10)
		fmt.Println("sasa= ", opt["offset"])
		time.Sleep(1000 * time.Millisecond)
	}
}