1 /** 2 * Copyright © The Bot Blog 2019 3 * License: MIT (https://github.com/TheBotBlog/thebotbloglib/blob/master/LICENSE) 4 * Author: Jacob Jensen (bausshf) 5 */ 6 module thebotbloglib.facebookservice; 7 8 import std.string : strip; 9 10 import thebotbloglib.webmanager; 11 import thebotbloglib.graphapi; 12 import thebotbloglib.facebookpost; 13 14 /// A Facebook service. 15 final class FacebookService 16 { 17 private: 18 /// The page id. 19 string _pageId; 20 /// The token. 21 string _token; 22 /// The comment rate limit. 23 size_t _commentRateLimit; 24 25 public: 26 final: 27 /** 28 * Creates a new Facebook service. 29 * Params: 30 * pageId = The id of the page. 31 * token = The token. 32 * commentRateLimit = (optional) (default = 5000) The rate limit for comments in milliseconds. 33 */ 34 this(string pageId, string token, size_t commentRateLimit = 5000) 35 { 36 _pageId = pageId; 37 _token = token; 38 _commentRateLimit = commentRateLimit; 39 } 40 41 @property 42 { 43 /// Gets the page id. 44 string pageId() { return _pageId; } 45 46 /// Gets the token. 47 string token() { return _token; } 48 49 /// Gets the comment rate limit. 50 size_t commentRateLimit() { return _commentRateLimit; } 51 } 52 53 /** 54 * Creates a post. If no photo is specified then an empty 400x1 image is attached. 55 * Params: 56 * message = The message of the post. 57 * photo = (optional) The photo of the post. This must be an URL. 58 * Returns: 59 * The newly created Facebook post. 60 */ 61 FacebookPost createPost(string message, string photo = null) 62 { 63 auto web = new WebManager(this); 64 65 string[string] data; 66 data["caption"] = message; 67 68 if (!photo || !photo.strip.length) 69 { 70 data["url"] = "https://cdn.discordapp.com/attachments/577173321054027784/631755165644357643/400x1.png"; 71 } 72 else 73 { 74 data["url"] = photo; 75 } 76 77 auto resp = web.postRequest!GraphAPIObjectResponse(pageId, "photos", data); 78 79 return resp ? new FacebookPost(this, resp.id) : null; 80 } 81 82 /** 83 * Retrieves a Facebook post. 84 * Params: 85 * id = The id of the post to retrieve. 86 * readPost = A boolean determining whether the post information should be read or not. 87 * Returns: 88 * The retrieved Facebook post. 89 */ 90 FacebookPost retrievePost(string id, bool readPost = false) 91 { 92 auto post = new FacebookPost(this, id); 93 94 if (readPost) 95 { 96 post.updatePostInfo(); 97 } 98 99 return post; 100 } 101 }