Rails 4.2 send email after post published - email

I have a simple Rails 4.2 blogging app.
I have created a boolean called 'published'. Only the published articles are shown on the blog.
I would like to send subscribers an email when a new post is marked 'published'.
I have the subscriptions set up through a Mailchimp list and I send the emails with Mandrill.
How can I get an email to send after I check 'published' on a post? Your help is greatly appreciated!

Without seeing your code, something like this might work
def publish
#post = Post.find(params[:id])
#post.publish
begin
#mailer = PublishedNotificationMailer.published_email(#post).deliver
render :json => '', :status => 200
rescue
render :json => '', :status => 422
end
end

Related

Is it possible to send a private message via Facebook GRAPH API?

I really need a solution. Facebook messages seem to be "read-only".
I use javascript.
f.e. {conversation-id}/messages?message={"hello there"}
(not working)
You can use their chat API until April 2015. After that, its gone.
No, you can't send private message via Facebook GRAPH API, you can't do that via FQL too, but there is a solution:
There is a way to send message to user's primary inbox, you need a library which is completely open source (by me) then:
<?php
require_once('send.message.php'); //requires file
require_once('facebook.php'); //sdk provided by facebook, you need one for tokens and
//all
$config = array('appId' => '1496661733888719' ,'secret'=>'0c959ab2dec71c5a53aab1cd64751223' );
$facebook=new Facebook($config);
if(!$facebook->getUser()){
$params = array(
'scope' => 'read_mailbox, xmpp_login',
'redirect_uri' => 'http://localhost/'
);
$url=$facebook->getLoginUrl($params);
header("location:".$url);
die();
}
$messageobj=new SendMessage($facebook);
$receiverId=''; // this may either be username or userID, this class takes care of both the //cases
$body='test message';
if($messageobj->sendMessage($body,$receiverId))
{
echo 'message sent';
}else
{
echo 'some error occured';
}
?>
In this way you can send message to users, you can get the detailed information on this at:
nishgtm.com/2013/11/facebook-message-api-php/
You will also find the link to github page on the above link. This library is super easy to use. Feel free to ask me anything via comments.

How to send direct message from Page with Attachment via Facebook Graph API?

I can manage Page Conversations (read, write) in help with Facebook Graph API. But it seems to be impossible to add any attachments to my direct messages.
https://developers.facebook.com/docs/graph-api/reference/conversation/messages
I see only "message" parameter in documentation. So, is there any way how to do it?
As #Niraj Shah mentioned above, the attachment sending feature is undocumented (for the time of this post, GraphAPI v2.12), but exists and works if you will post the source field:
PHP:
$fb =
new Facebook([
'app_id' => 'your app id',
'app_secret' => 'your app secret',
'default_graph_version' => 'v2.12',
'default_access_token' => 'your page token',
]);
$response =
$fb->post(
"/{$conversationId}/messages",
[
'message' => '',
'source' => $fb->fileToUpload($attachmentFileName),
]
);
The message field can be empty to send an attachment only.
As the documentation points out, the API only supports the message parameter. So only text can be sent in a message, and attachments are not supported. However, you can try sending the source or url parameter in your API call and see if Facebook adds it to the message (it could be a undocumented feature).

Facebook wall post error: OAuthException :: (#1500) The url you supplied is invalid

I have a web-based news app that runs on Heroku. When users post a comment to a news story on my app, my app forwards the comment to the user's facebook wall using fb_graph. Everything worked perfectly until a couple of weeks ago. For no reason that I can explain I am now seeing some baffling behavior.
Now, when a user submits a comment to a story the FB API responds with, OAuthException :: (#1500) The url you supplied is invalid. If, the same user then submits additional comments to the same story those comments are posted to the user's FB feed just fine.
I have used the FB Graph API explorer to confirm that I have valid access tokens, and that my app does accept posts to the token-owner's FB feed.
To make things even more baffling, when running my web app in development on localhost all of the posts go through just fine to my development FB app.
def post_to_facebook(story, post)
auth = Authentication.find_by_provider_and_user_id("facebook", current_user.id)
if auth
me = FbGraph::User.me(auth.token)
if me.permissions.include?(:publish_stream)
begin
me.feed!(
:message => "#{best_name(current_user)} made the following post to NewsWick: #{post.contents}",
:name => story.title,
:link => "https://www.newswick.com/stories/"+story.id.to_s,
:description => "Story posted to the NewsWick world-wide news service" ,
:picture => best_photo(story)[:photo_url]
)
rescue => e
#msg = "Facebook posting error: "+ e.to_s
puts "Facebook feed posting error: #{e.message}"
end
else
#msg = "No longer authorized to post to Facebook."
end
end
return #msg
end
One last thing to note, the only thing that I have changed w/r/t how my app interacts with FB in the last two weeks was that i accepted FB's July Breaking Changes.
Anyone have any clues. This is driving me bonkers!!!
I'm having the same issue only difference is I'm using the javascript api.
Seems like it's a facebook bug, which is already reported here: https://developers.facebook.com/bugs/136768399829531
Yes this is a known bug and Facebook developers are looking into it, well so they claim,however something interesting I found out is:
I post to my Facebook using 2 methods using RestFB API , first, for messages with URLs e.g www.something.com and those without URLs, I realized last night that all posts without URL worked and the ones with URL doesn't.
So I changed all my implementation to send messages to Facebook without using with link parameters for all posts, with or without links.
With link Parameter - throws error #1500
FacebookType publishMessageResponse = resftFBclient.publish(FACEBOOK_PAGE_ID
+"/feed", FacebookType.class, Parameter.with("message", "Hello StackOverFlow!"),
Parameter.with("link", "message with a link , www.me.com"));
With no link parameter - this works even if message contained URL/link
FacebookType publishMessageResponse = resftFBclient.publish(FACEBOOK_PAGE_ID. +
"/feed",FacebookType.class,Parameter.with("message", "My message"));
This works even if message contained URL/link and it creates an clickable link on FB. Could it be that FB is trying to drop the link implementation and letting us figure it out that the former works just as the link implementation? What's the difference anyways?
That's brutal!
Cheers
Babajide
I was trying to solve this problem this problem that seems to be occurring to almost everyone.
I am using the PHP SDK.
What I noticed is that it always returned this error for the first time I tried to post the link. On a second try, it was posted with success.
Really hackishly I then checked for an error and retried to post to the wall.
$errorCount = 0;
function postPicture($phrase)
{
try
{
$image = $_SESSION['photoLink'];
$facebook->setFileUploadSupport(true);
$response = $facebook->api(
'/me/feed',
'post',
array(
'message' => $phrase,
'picture' => 'http://mylink/pictures/facebook.png',
'link' => $image,
'caption' => 'My caption',
'description' => 'My description',
'type' => 'photo',
'name' => 'My name'
)
);
echo 'Success';
}
}
catch (FacebookApiException $e)
{
// You really should check if this $error is #1500 before doing that. I didn't :)
if($errorCount < 2)
{
postPicture($phrase);
$errorCount++;
}
else
{
$e = str_replace('"', "", $e);
$e = str_replace("'", "", $e);
echo 'Error ' . $e;
}
}
}
To solve these problems just make sure you add these og metadata tags in the head section of the page represented by the url you want to share:
<meta property="og:type" content="article" /> //or any other type like blog, website etc....
<meta property="og:url" content="your article url here" />
<meta property="og:title" content="your article title here" />
Good luck!

PHP Facebook wallpost tracking

I have such code to post on my friends wall from application
$args = array(
‘message’ => 'my message',
‘name’ => ‘This is my demo Facebook application!’,
‘caption’ => “Caption of the Post”,
‘link’ => $data['view_url'],
‘description’ => ‘this is a description’,
‘picture’ => $body_all["thumb"],
‘actions’ => array(array(‘name’ => ‘View’,
‘link’ => $data['view_url']))
);
$post_id = $facebook->api(“/”.user_$id.”/feed”, “post”, $args);
In array $post_id['id'] facebook returned me id of the post(like that: 100003284224348_121820851270722), and then when i wona to track it, and get some info by ID, i ask FBgraph https://graph.facebook.com/$post_id['id'] but its return false… i have publish_permission, read_streem permission in my application… what i do wrong? why i couldn`t see post in info with id on Facebook graph?
If the graph API GET request is returning false this is usually a tell tell sign that there is an access token issue at the heart of the problem. When you are making the GET request ensure you are using your applications access token.
Try it here: http://developers.facebook.com/tools/explorer/?method=GET&path=100003284224348_121820851270722
It could be a timing issue, where you get back the ID and try to use it before facebook's API can see it. Try waiting a moment and checking. Does it come up in the graph API explorer tool?

Facebook Posting API Question

on my site right now I'm trying to have it post to a users wall a media file.
I had it working on the old api before, but now I'm trying to get it working on the new one and I'm having an issue.
I'm running this
$facebook->api_client->stream_publish($message, $attachment, $action_links);
Is that the old api or the new one? Because I'm getting this error
Call to a member function stream_publish() on a non-object in
I was reading a tutorial and it said to do this
$statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> 'the message', 'cb' => ''));
but how can I post an attachment/ action links using that?
Thanks!
First of all, don't confuse APIs with SDKs. The newest PHP SDK is capable of communicating with both the new Graph API and the old REST API.
And you are using the new SDK, since Facebook::$api_client doesn't exist in the new SDK.
Secondly, as is usual with most tutorials, they only show you a snapshot of the full features of a system. See here for more details about publishing with the Graph API.
However, you can still use the old API to publish your message like so
$facebook->api( array(
'method' => 'stream.publish'
, 'target_id' => $facebook->getUser()
, 'message' => $message
, 'attachment' => $attachment
, 'action_links' => $action_links
) );