-
-
Save Yardanico/c20e3ed232d7b47ec39786900c2d6b23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils, httpclient, irc, asyncdispatch, json, streams | |
const | |
GitterToken = "396654d3d0c1dcadfddaaa63a6129a44e3507439" | |
RoomId = "59ea3bdbd73408ce4f7af994/chatMessages" | |
StreamApiUrl = "https://stream.gitter.im/v1/rooms/$1/chatMessages" | |
GetRoomUrl = "https://api.gitter.im/v1/rooms" | |
proc getRoomId(client: AsyncHttpClient, uri: string): Future[string] {.async.} = | |
let headers = newHttpHeaders({ | |
"content-type": "application/json", | |
"authorization": "Bearer $1" % GitterToken | |
}) | |
let data = %*{"uri": uri} | |
let resp = await client.request(GetRoomUrl, HttpPost, $data, headers) | |
result = parseJson(await resp.body)["id"].getStr() | |
proc main() {.async.} = | |
let headers = newHttpHeaders({ | |
"authorization": "Bearer $1" % GitterToken | |
}) | |
let client = newAsyncHttpClient() | |
let id = await client.getRoomId("testcomnim/lobby") | |
echo "Connected to room with id: ", id | |
var resp = await client.request(StreamApiUrl % id, HttpGet, headers=headers) | |
var stream = resp.bodyStream | |
stream.callback = | |
proc (streamFut: FutureStream[string]) = | |
# The callback is also called when the future is completed. So you should | |
# use ``finished`` to check whether data is available. | |
if streamFut.finished: | |
echo "Finished?" | |
var finalFut: Future[(bool, string)] = streamFut.read() | |
# I'm trying to set a callback for finalFut future here | |
finalFut.callback = | |
proc (fut: Future[(bool, string)]) = | |
let (hasValue, data) = fut.read() | |
if hasValue: | |
echo repr data | |
let parsed = parseJson(data) | |
let username = parsed["fromUser"]["username"].getStr() | |
let text = parsed["text"].getStr() | |
echo "$1 wrote: `$2`" % [username, text] | |
waitFor main() | |
runForever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment