简体   繁体   中英

How to send facebook message from linux console (command-line)

Old days many admins use sms-gates for sending important informations from their systems eg "Power down, UPS is working now!", "Power Up, UPS is off!" or "CPU Temp too high!". Today in Facebook era we use messenger instead of SMS, so I wonder if I could create a command-line bash or php script for such thing.

The idea - cron checks every 10 minutes the condition and if it is true, sends message to my messenger.

The issues:

  1. I don't want to use my fb account for sending - I'd like to get message from "System 1", "System 2", because i have more than one system to admin.
  2. The bash part is easy for me, I need tips for Facebook solutions:
    • do I have to get FacebookAppId (and do I have to create AppId for each system or just one AppId)
    • how to "join/confirm/accept" "System 1" account with my Facebook Account
    • is it possible to send messages to more than one FBAccount
    • any other hints what should i look for.

I found Notification App, but i think that it doesn't send message to messenger, so it would be useless.

The Chat API was removed with v2.0 of the Graph API, there is no way to send messages with an API anymore. Btw, messages are for communcation between real users, they should not be used as notification system anyway. SMS is still a good option for those kind of warnings imho.

Using a Page and the /conversations endpoint would not work either:

Pages can only reply to a message - they cannot initiate a conversation. Also, a Page can only respond twice to a particular message, the other party will have to respond before they can reply again.

Source: https://developers.facebook.com/docs/graph-api/reference/v2.3/conversation/messages#publish

I think for your special purpose, twitter may be a better option. Twitter accepts tweets from API. So what you need to do is to set up an account to publish your system status either regularly or event-triggingly and follow it in your own personal account. And there are already plenty of open source projects focusing on tweeting via API, and t is the one I am currently using.

I just published a service exactly for that use case : https://www.nimrod-messenger.io/

It's at an early stage. Feedbacks are more than welcome :-)

So there are a couple of command line apps to do this.

There is a libpurple extension ( https://github.com/dequis/purple-facebook ) which works. However purple doesn't seem to support the idea of message history. This is a shame since I imagine offline messages is the default way most people use facebook.

There is an single use command tool for facebook as well: https://www.npmjs.com/package/fb-messenger-cli which does support history. Unfortunately this is a TUI rather than a command line application and doesn't seem to depend on a separate facebook library. Some hacking or terrible expect glue could work around this.

Sending facebook message in bash script

IDEA

I needed a script (which work on my work/local mac) that checks server. If there are problems, script will send me messages on Facebook.

Dependencies

need to install: https://github.com/mjkaufer/Messer

Solution, bash script

FB_SENDER_LOGIN=""
FB_SENDER_PASSWORD=""

function send_fb_message {
    FB_MESSER_COMAND="messer $FB_SENDER_LOGIN $FB_SENDER_PASSWORD --command='m \"$1\" $2'"
    eval "$FB_MESSER_COMAND"
}

RECIPIENT_NAME_DISPLAYED_IN_FACEBOOK_WEBSITE="Vasily Bodnarchuk"
MESSAGE="Houston, we have a problem!!!"
send_fb_message "$RECIPIENT_NAME_DISPLAYED_IN_FACEBOOK_WEBSITE" "$MESSAGE"

Automated Facebook message

I was looking for something like this exactly and found that Messer is the way to go.

Solution

Look at this repo called Messer https://github.com/mjkaufer/Messer
(it works with 2FA too but not with app passwords)

Implementation

  1. See Readme

  2. I needed it to run automatically so I used it's non-interactive mode with a bash script:

message="Hey, what\'s up bro"
FULLPATH/node_modules/.bin/messer --command="m \"Myfriends Name\" $message"
  • I don't like to install something like this globally through NPM so I used the /node_modules/.bin/messer executable in the project's folder.
  • Used double quotes escaped instead of single quotes to be able to use variables inside command.
  • Drawback: Messer can only send, not receive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM