Example #1
0
// Run executes the bot and connects to Telegram using the provided token. Use the debug flag if you wish to see all trafic logged
func Run(token string, debug bool) {
	var err error
	tg, err = tgbotapi.NewBotAPI(token)
	if err != nil {
		log.Fatal(err)
	}

	tg.Debug = debug

	log.Printf("Authorized on account %s", tg.Self.UserName)

	u := tgbotapi.NewUpdate(0)
	u.Timeout = 60

	err = tg.UpdatesChan(u)
	if err != nil {
		log.Fatal(err)
	}

	bot.New(&bot.Handlers{
		Response: responseHandler,
	})

	for update := range tg.Updates {
		target := strconv.Itoa(update.Message.Chat.ID)
		sender := strconv.Itoa(update.Message.From.ID)
		bot.MessageReceived(target, update.Message.Text, &bot.User{Nick: sender})
	}
}
Example #2
0
// Run connects to slack RTM API using the provided token
func Run(token string) {
	api = slack.New(token)
	rtm = api.NewRTM()

	bot.New(&bot.Handlers{
		Response: responseHandler,
	})

	go rtm.ManageConnection()

Loop:
	for {
		select {
		case msg := <-rtm.IncomingEvents:
			switch ev := msg.Data.(type) {
			case *slack.MessageEvent:
				bot.MessageReceived(ev.Channel, ev.Text, extractUser(ev.User))

			case *slack.RTMError:
				fmt.Printf("Error: %s\n", ev.Error())

			case *slack.InvalidAuthEvent:
				fmt.Printf("Invalid credentials")
				break Loop
			}
		}
	}
}
Example #3
0
func onPRIVMSG(e *ircevent.Event) {
	bot.MessageReceived(e.Arguments[0], e.Message(), &bot.User{Nick: e.Nick})
}