Skip to content

Instantly share code, notes, and snippets.

@pintowar
Created August 1, 2019 00:43
Show Gist options
  • Select an option

  • Save pintowar/afb58e2f5a2def148cb53c773b42b2c4 to your computer and use it in GitHub Desktop.

Select an option

Save pintowar/afb58e2f5a2def148cb53c773b42b2c4 to your computer and use it in GitHub Desktop.
Minimalist telegram chat bot
@Grab('com.github.pengrad:java-telegram-bot-api:4.4.0')
import com.pengrad.telegrambot.TelegramBot
import com.pengrad.telegrambot.UpdatesListener
import com.pengrad.telegrambot.request.SendMessage
def bot = new TelegramBot("my:token") //token
def chats = [] as Set //chatIds as Long
bot.setUpdatesListener { updates ->
// ... process updates
// return id of last processed update or confirm them all
//chat=Chat{id=108569910
chats += (updates.collect { it.message().chat().id() } as Set)
chats.each { chatId ->
println "To chat $chatId"
sendMessage(bot, chatId, 'Hello Master!!')
}
return UpdatesListener.CONFIRMED_UPDATES_ALL;
}
def sendMessage(TelegramBot bot, Long chatId, message) {
def request = new SendMessage(chatId, message)
bot.execute(request)
}
println "Waiting..."
synchronized (this) { this.wait() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment