Ejemplo n.º 1
0
// for processing incoming update from Telegram
func processUpdate(b *bot.Bot, update bot.Update) bool {
	// check username
	var userId string
	if update.Message.From.Username == nil {
		log.Printf("*** Not allowed (no user name): %s\n", *update.Message.From.FirstName)
		return false
	}
	userId = *update.Message.From.Username
	if !isAvailableId(userId) {
		log.Printf("*** Id not allowed: %s\n", userId)
		return false
	}

	if update.HasMessage() {
		txt := *update.Message.Text

		if isVerbose {
			log.Printf("received telegram message: %s\n", txt)
		}

		if strings.HasPrefix(txt, "/") { // if it is command,
			if strings.HasPrefix(txt, "/"+lib.CommandStart) {
				message := lib.MessageStart
				var options map[string]interface{} = map[string]interface{}{
					"reply_markup": bot.ReplyKeyboardMarkup{
						Keyboard: allKeyboards,
					},
				}

				// send message
				if sent := b.SendMessage(update.Message.Chat.Id, &message, options); !sent.Ok {
					log.Printf("*** Failed to send message: %s\n", *sent.Description)
				}
			} else if strings.HasPrefix(txt, "/"+lib.CommandTime) {
				time := lib.GetTimeString()

				queue <- time

				if sent := b.SendMessage(update.Message.Chat.Id, &time, map[string]interface{}{}); !sent.Ok {
					log.Printf("*** Failed to send message: %s\n", *sent.Description)
				}
			} else if strings.HasPrefix(txt, "/"+lib.CommandIP) {
				ip := lib.GetIPString()

				queue <- ip

				if sent := b.SendMessage(update.Message.Chat.Id, &ip, map[string]interface{}{}); !sent.Ok {
					log.Printf("*** Failed to send message: %s\n", *sent.Description)
				}
			} else if strings.HasPrefix(txt, "/"+lib.CommandHelp) {
				// send message
				message := lib.MessageHelp
				if sent := b.SendMessage(update.Message.Chat.Id, &message, map[string]interface{}{}); !sent.Ok {
					log.Printf("*** Failed to send message: %s\n", *sent.Description)
				}
			} else {
				log.Printf("*** No such command: %s\n", txt)

				// send message
				message := fmt.Sprintf("No such command: %s", txt)
				if sent := b.SendMessage(update.Message.Chat.Id, &message, map[string]interface{}{}); !sent.Ok {
					log.Printf("*** Failed to send message: %s\n", *sent.Description)
				}
			}
		} else { // otherwise,
			queue <- txt
		}
	}

	return false
}
Ejemplo n.º 2
0
}

// for processing incoming request through HTTP
var httpHandler = func(w http.ResponseWriter, r *http.Request) {
	command := r.FormValue(lib.ParamCommand)
	value := r.FormValue(lib.ParamValue)

	if isVerbose {
		log.Printf("received http command: %s, value: %s\n", command, value)
	}

	if command != "" { // if it is command,
		if command == lib.CommandTime {
			queue <- lib.GetTimeString()
		} else if command == lib.CommandIP {
			queue <- lib.GetIPString()
		} else {
			log.Printf("*** No such command: %s\n", command)
		}
	} else { // otherwise,
		queue <- value
	}
}

func main() {
	client := bot.NewClient(apiToken)
	client.Verbose = isVerbose

	pHat = scroll.New()
	if pHat == nil {
		panic("Failed to initialize Scroll pHat")