Instant messaging with FarmBot

@mdingena This is very doable, especially if you write it in NodeJS, since FarmBotJS works in node.

If you want to write it in another language, such as Java, Ruby, etc, this is also possible, as long as there are MQTT clients available. Let me know which language you plan on supporting and I can help you identify a client.

Here is how you could build a FarmBot chat app (high level overview):

Device -> IM client

  1. Generate an API token.
  2. Give the token to FarmbotJS -or- if you don’t want to use FarmBotJS, Point your MQTT client of choice to mqtt.farmbot.io with your email/token as credentials.
  3. Subscribe to the appropriate MQTT channel, which may differ based on your use case. Example: Subscribing to bot/device_23/logs would give you realtime logs for the device with an id of 23 (if your token was for device 23). Subscribing to bot/device_23/# will give you everything, unfiltered (useful if debugging). It will be a JSON object. See my note below about RPC formats.
  4. Tell your chat service to intercept relevant MQTT messages and forward them to whatever relevant chat protocol you use (IRC, XMPP, etc…)

IM -> Client

  1. Listen to the relevant IRC/XMPP/Whatever client.
  2. When an event is triggered, publish an RPC node to bot/device_23/from_clients (see my note below). This channel is reserved for inbound commanes

Note about RPC commands:

All Farmbot software (the frontend, FarmbotJS, the API, the MQTT broker, FBOS, etc…) shares a common format for sending/receiving realtime data and RPC. Internally, we have called it “CeleryScript”. A good example of CeleryScript usage is the sequence editor in the web app.

When you build a sequence in your browser, you are actually building a CeleryScript AST, which is later processed by FarmBotJS.

We don’t have any formal documentation for CeleryScript nodes yet for the simple reason that no one has asked. I can write such documentation if you require it. If you plan on using FarmBotJS, you will not need to dive into the specifics of CeleryScript since it is abstracted away. If you write the app in TypeScript, you will get intellisense / method documentation also.

Please let me know if that points you in the right direction. Like I mentioned, I would be more than willing to write documentation about the RPC commands if you end up not using the FarmBotJS client.

3 Likes