Exemplo n.º 1
0
func (p *providerTelegram) intakeLoop() {
	u := tgbotapi.NewUpdate(0)
	u.Timeout = 60
	updates, err := p.tg.GetUpdatesChan(u)
	if err != nil {
		p.err = err
		return
	}

	go func(upds <-chan tgbotapi.Update) {
		log.Println("telegram: started message intake loop")
		for {
			for update := range upds {
				senderID := strconv.Itoa(update.Message.From.ID)
				senderName := update.Message.From.FirstName
				targetID := strconv.FormatInt(update.Message.Chat.ID, 10)
				targetName := update.Message.Chat.FirstName
				msg := messages.Message{
					Room:         "",
					FromUserID:   senderID,
					FromUserName: senderName,
					ToUserID:     targetID,
					ToUserName:   targetName,
					Message:      update.Message.Text,
					// todo(carlos): do DM detection
					// Direct:     strings.HasPrefix(data.Channel, "D"),
				}
				p.in <- msg
			}
		}
	}(updates)
}
Exemplo n.º 2
0
func updateHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	bot, err := botApi.NewBotAPIWithClient(TOKEN, urlfetch.Client(appengine.NewContext(r)))
	num, err := strconv.Atoi(r.URL.Query().Get("offset"))
	updates, err := bot.GetUpdates(botApi.NewUpdate(num))
	resp, err := json.Marshal(updates)
	checkErr(w, err)
	fmt.Fprint(w, string(resp))
}