Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 2.91 KB | None | 0 0
  1. import sugar
  2. import strutils
  3.  
  4. type
  5.   Credentials {.importc.} = ref object
  6.     email {.importc.}: cstring
  7.     password {.importc.}: cstring
  8.   Api {.importc.} = ref object
  9.   Error {.importc.} = ref object
  10.     error: cstring
  11.   FnApiCallback = proc(error: Error, api: Api) : Error {.closure.}
  12.   FnLogin = proc(info: Credentials, callback: FnApiCallback): void {.closure.}
  13.   MessageObject {.importc.} = ref object #TODO: Implement other fields when needed.
  14.     body: cstring
  15.   Event {.importc.} = ref object
  16.       body: cstring #TODO: Implement other fields when needed.
  17.       threadID: uint
  18.       senderID: uint
  19.       isGroup: bool
  20.       `type`: string
  21.   UserInfo {.importc.} = ref object
  22.     fullName: cstring
  23.     userID: uint
  24.     alternateName: cstring
  25.     firstName: cstring
  26.     gender: cstring
  27.     isFriend: bool
  28.     profilePicture: cstring
  29.     `type`: cstring
  30.     profileUrl: cstring
  31.     vanity: cstring
  32.     isBirthday: bool
  33.   FnListen = proc(error: Error, event: Event): Error {.closure.}
  34.   FnGetFriendsList = proc(error: Error, data: seq[UserInfo]): Error {.closure.}
  35.  
  36.  
  37. {.push importcpp.}
  38. proc getCurrentUserID(api: Api): int
  39. proc listen(api: Api, callback: FnListen): Error
  40. proc sendMessage(api: Api, message: cstring, thread_id: uint, callback: proc = nil, message_id: cstring = nil): void
  41. proc getFriendsList(api: Api, callback: FnGetFriendsList): void
  42. {.pop.}
  43.  
  44. {.push importc.}
  45. proc require(module: cstring): FnLogin
  46. {.pop.}
  47.  
  48. var friends_list: seq[UserInfo] = @[]
  49. const whitelist_name: array[4, string] = ["zin", "anas", "ranj", "mouhamed"]
  50. var whitelist_list: seq[uint] = @[]
  51.  
  52. proc getFriendId(list: seq[UserInfo], friend: string): uint =
  53.   var i: uint = 0
  54.   for element in list:
  55.     if cast[string](element.fullName).toLowerAscii.contains(friend):
  56.       i = element.userID
  57.       break
  58.  
  59.   return i
  60.  
  61. proc on_message_received(error: Error, event: Event, api: Api): Error =
  62.   if error != nil: echo error.error; return error
  63.   echo "event_type_msg_received: " & event.type
  64.  
  65.   if event.type == "message":
  66.     if event.body == "Die!": return error
  67.     echo event.isGroup
  68.     if event.isGroup == false:
  69.       for element in whitelist:
  70.         let id = friends_list.getFriendId(element)
  71.         echo element & ':' & $(id.int)
  72.         echo event.senderID.int
  73.         if event.senderID == id:
  74.           api.sendMessage(event.body, event.threadID)
  75.      
  76.  
  77.  
  78. proc on_login(error: Error, api: Api): Error =
  79.   if error != nil: return error
  80.   api.getFriendsList((error, data) => (if error != nil: return error; (
  81.     for i in 0..(data.len-1):
  82.       friends_list.add(UserInfo(fullName: data[i].fullName, userID: data[i].userID))
  83.     )))
  84.  
  85.   echo "flist: " & $friends_list.len
  86.   var state = api.listen((error, event) => on_message_received(error, event, api))
  87.  
  88. let login = require("facebook-chat-api")
  89. login(Credentials(email: "paste_bin@pasta.kappa", password: "on_my_dead_body"), on_login)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement