telebot is a Telegram Bot API implementation for Dart, you can use this library
to execute Telegram Bot API by easy ways, see Telegram Bot API in Telegram Bot Document.
dependencies:
telebot: ^1.0.0import 'package:telebot/telebot.dart';See here or follow steps below
All Telegram Bot's action is by talking with BotFather to complete, so you need to browse https://t.me/botfather to add BotFather as contact
Open Telegram and find BotFather, then send any message to him, he will tell you all what can he do
Send /newbot to him and follow the steps, final you will create a bot
Send /mybots to him then choose your bot, then choose option API Token, final you will get token
var bot = TelegramBot.init("BOT_TOKEN");var bot = TelegramBot.init("BOT_TOKEN");
bot.sendMessage(chateId: "CHAT_ID", text: "Hello World").then((Message messageResult){
// got result
}).catchError((error){
// handle error
});or use async/await
var bot = TelegramBot("BOT_TOKEN");
Message messageResult = await bot.sendMessage(chateId: "CHAT_ID", text: "Hello World");All Telegram Bot API was implemented in telebot, see Telegram Bot Document.
var bot = TelegramBot("BOT_TOKEN");
await bot.sendMessage(
chatId: "CHAT_ID",
text: "Choose One!",
replyMarkup: InlineKeyboardMarkup(
inlineKeyboard: [
[
InlineKeyboardButton(text: "Apple", callbackData: "1000"),
InlineKeyboardButton(text: "Banana", callbackData: "1001"),
]
],
),
);Now telebot only support String url.
var bot = TelegramBot.init(BOT_TOKEN);bot.onMessage((message)){
if(message.from.isBot) return;
message.reply("Hello! I'm a bot.");
}.onEditedMessage((message){
// do something
});TelegramBot supported events
| Event | Parameter |
|---|---|
| onUpdate | Update |
| onMessage | Message |
| onEditedMessage | Message |
| onChannelPost | Message |
| onEditedChannelPost | Message |
| onInlineQuery | InlineQuery |
| onChosenInlineResult | ChosenInlineResult |
| onCallbackQuery | CallbackQuery |
| onShippingQuery | ShippingQuery |
| onPreCheckoutQuery | PreCheckoutQuery |
| onPoll | Poll |
| onPollAnswer | PollAnswer |
bot.startServer(host: "YOUR SERVER HOST", port: SERVER_PORT);- Clone this project
git clone https://github.com/Arxing/dart-telebot.git
- Edit
example/env.dartand inputBOT_TOKEN,SERVER_PORTandWEBHOOK_URL - Run
example/update_webhook.dartto set bot's webhook urldart ./example/update_webhook.dart
- Run
example/start_bot_server.dartto start listeningdart ./example/start_bot_server.dart
- Now talk to your bot in Telegram, then it will reply you same message!