Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sugar
- import strutils
- type
- Credentials {.importc.} = ref object
- email {.importc.}: cstring
- password {.importc.}: cstring
- Api {.importc.} = ref object
- Error {.importc.} = ref object
- error: cstring
- FnApiCallback = proc(error: Error, api: Api) : Error {.closure.}
- FnLogin = proc(info: Credentials, callback: FnApiCallback): void {.closure.}
- MessageObject {.importc.} = ref object #TODO: Implement other fields when needed.
- body: cstring
- Event {.importc.} = ref object
- body: cstring #TODO: Implement other fields when needed.
- threadID: uint
- senderID: uint
- isGroup: bool
- `type`: string
- UserInfo {.importc.} = ref object
- fullName: cstring
- userID: uint
- alternateName: cstring
- firstName: cstring
- gender: cstring
- isFriend: bool
- profilePicture: cstring
- `type`: cstring
- profileUrl: cstring
- vanity: cstring
- isBirthday: bool
- FnListen = proc(error: Error, event: Event): Error {.closure.}
- FnGetFriendsList = proc(error: Error, data: seq[UserInfo]): Error {.closure.}
- {.push importcpp.}
- proc getCurrentUserID(api: Api): int
- proc listen(api: Api, callback: FnListen): Error
- proc sendMessage(api: Api, message: cstring, thread_id: uint, callback: proc = nil, message_id: cstring = nil): void
- proc getFriendsList(api: Api, callback: FnGetFriendsList): void
- {.pop.}
- {.push importc.}
- proc require(module: cstring): FnLogin
- {.pop.}
- var friends_list: seq[UserInfo] = @[]
- const whitelist_name: array[4, string] = ["zin", "anas", "ranj", "mouhamed"]
- var whitelist_list: seq[uint] = @[]
- proc getFriendId(list: seq[UserInfo], friend: string): uint =
- var i: uint = 0
- for element in list:
- if cast[string](element.fullName).toLowerAscii.contains(friend):
- i = element.userID
- break
- return i
- proc on_message_received(error: Error, event: Event, api: Api): Error =
- if error != nil: echo error.error; return error
- echo "event_type_msg_received: " & event.type
- if event.type == "message":
- if event.body == "Die!": return error
- echo event.isGroup
- if event.isGroup == false:
- for element in whitelist:
- let id = friends_list.getFriendId(element)
- echo element & ':' & $(id.int)
- echo event.senderID.int
- if event.senderID == id:
- api.sendMessage(event.body, event.threadID)
- proc on_login(error: Error, api: Api): Error =
- if error != nil: return error
- api.getFriendsList((error, data) => (if error != nil: return error; (
- for i in 0..(data.len-1):
- friends_list.add(UserInfo(fullName: data[i].fullName, userID: data[i].userID))
- )))
- echo "flist: " & $friends_list.len
- var state = api.listen((error, event) => on_message_received(error, event, api))
- let login = require("facebook-chat-api")
- login(Credentials(email: "paste_bin@pasta.kappa", password: "on_my_dead_body"), on_login)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement