hqtoxbot/src/hqtoxbot.nim

75 lines
1.9 KiB
Nim
Raw Normal View History

2019-12-17 04:53:51 +01:00
import toxcore
import std/asyncdispatch, std/json, std/httpclient
const spaceApiUrl = "http://spaceapi.hq.c3d2.de:3000/spaceapi.json"
const adminIds = [
toAddress "DF0AC9107E0A30E7201C6832B017AC836FBD1EDAC390EE99B68625D73C3FD929FB47F1872CA4"
# Emery
]
proc bootstrap(bot: Tox) =
const servers = [ ("tox.neuland.technology", "15E9C309CFCB79FDDF0EBA057DABB49FE15F3803B1BFF06536AE2E5BA5E4690E".toPublicKey ) ]
for host, key in servers.items:
bot.bootstrap(host, key)
proc addAdmin(bot: Tox; id: Address) =
discard bot.addFriend(
id, "You have been granted administrative rights to " & bot.name)
proc setupCallbacks(bot: Tox) =
#[
2019-12-23 18:50:36 +01:00
bot.onConferenceInvite do (friend: Friend; kind: ConferenceType;
2019-12-17 04:53:51 +01:00
cookie: string):
discard bot.join(friend, cookie)
]#
2019-12-23 18:50:36 +01:00
bot.onFriendMessage do (f: Friend; msg: string; kind: MessageType):
2019-12-17 04:53:51 +01:00
echo msg
for id in adminIds:
bot.addAdmin(id)
proc newBot(name: string): Tox =
result = newTox do (opts: Options):
opts.localDiscoveryEnabled = true
opts.ipv6Enabled = true
result.name = name
result.setupCallbacks()
result.bootstrap()
echo result.name, " is at ", result.address
proc updateStatus(bot: Tox; http: AsyncHttpClient) {.async.} =
try:
let
rsp = await http.get(spaceApiUrl)
body = await rsp.body
space = parseJson body
status = $(space["status"])
if bot.statusMessage != status:
echo "change status to ", status
bot.statusMessage = $(space["status"])
except:
echo "status update failed"
2019-12-23 18:50:36 +01:00
bot.statusMessage = "status update failed"
2019-12-17 04:53:51 +01:00
proc main() =
let
bot = newBot("HQ Bot")
http = newAsyncHttpClient()
echo "DHT port and key: ", bot.udpPort, " ", bot.dhtId
waitFor updateStatus(bot, http)
2019-12-17 04:53:51 +01:00
addTimer(20*1000, oneshot=false) do (fd: AsyncFD) -> bool:
asyncCheck updateStatus(bot, http)
while true:
iterate bot
poll bot.iterationInterval
main()