-
Notifications
You must be signed in to change notification settings - Fork 63
Using Swift REPL for calling API methods
A read–eval–print loop (REPL) allows executing arbitrary Swift commands and seeing their output instantly.
Clone the library and build any sample project:
git clone https://github.com/zmeyc/telegram-bot-swift.git
cd telegram-bot-swift/Examples/hello-bot
swift build
Currently there are unresolved issues with importing frameworks when running REPL directly, so as a workaround let's run it using lldb:
export HELLO_BOT_TOKEN="token obtained from BotFather"
lldb -o run .build/debug/hello-bot
Expected output:
(lldb) target create ".build/debug/hello-bot"
Current executable set to '.build/debug/hello-bot' (x86_64).
(lldb) run
endpoint: getMe, data:
Ready to accept commands
Send something to your bot and write down from.id
:
{
"update_id" : 642225283,
"message" : {
...
"from" : {
"username" : "username",
"id" : 78440479, <----------- from.id
"last_name" : "Lastname",
"first_name" : "Firstname"
}
}
}
Press CTRL-C
to pause execution, then at lldb prompt type:
repl
REPL prompt will appear:
1>
When using Xcode, you can skip all of this and simply place a breakpoint in main loop, then in debugger run
repl
.
Init a bot instance:
import TelegramBot
let bot = TelegramBot(token: readToken("HELLO_BOT_TOKEN"))
Bot is now ready to execute commands. Let's sum it up:
export HELLO_BOT_TOKEN="token obtained from BotFather"
lldb -o run .build/debug/hello-bot
CTRL-C
repl
import TelegramBot
let bot = TelegramBot(token: readToken("HELLO_BOT_TOKEN"))
Ready. Let's try some commands.
TAB
can be used for auto completing commands.
In real apps you'll want to use asynchronous versions of API methods, but for REPL synchronous ones are more convenient:
let chatId: Int64 = 12345678 // <- from.id you've written down
bot.sendMessageSync(chatId, "Hello")?.prettyPrint()
prettyPrint()
will print sendMessage
result with indentation, for this command it returns:
{
"chat" : {
"username" : "username",
"id" : 12345678,
"last_name" : "Lastname",
"type" : "private",
"first_name" : "Firstname"
},
"date" : 1465512130,
"from" : {
"id" : 12345678,
"first_name" : "TestBot",
"username" : "test_bot"
},
"message_id" : 886,
"text" : "Hello"
}
Also, "Hello"
will appear in Telegram Client.