mirror of
https://git.sr.ht/~ehmry/nim-toxcore
synced 2026-01-15 02:11:32 +00:00
No description
| LICENSES | ||
| src | ||
| tests | ||
| .gitignore | ||
| flake.lock | ||
| flake.nix | ||
| README.md | ||
| toxcore.nimble | ||
Nim wrapper over the Toxcore library
import toxcore
from std/os import sleep
const
bootstrapHost = "85.143.221.42"
bootstrapKey = "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43".toPublicKey
type Bot = ref object
tox: Tox
proc setupCallbacks(bot: Bot) =
var echoCount = 0
# bind a value for callback closure magic
bot.tox.onFriendRequest do (pk: PublicKey; msg: string):
discard bot.tox.addFriendNoRequest(pk)
bot.tox.onFriendMessage do (f: Friend; msg: string; kind: MessageType):
discard bot.tox.send(f, msg, kind)
inc echoCount
bot.tox.statusMessage = $echoCount & " echos served "
proc newEchoBot(name: string): Bot =
result = Bot(tox: initTox())
result.tox.name = name
result.setupCallbacks()
result.tox.bootstrap(bootstrapHost, bootstrapKey)
echo result.tox.name, " echos messages to ", result.tox.address
let
a = newEchoBot "alice"
b = newEchoBot "bob"
while true:
iterate a.tox
iterate b.tox
sleep min(a.tox.iterationInterval, b.tox.iterationInterval)