Documentation ¶
Overview ¶
Package imessage is used to interact with iMessage (Messages.app) on macOS
Use this library to send and receive messages using iMessage. Can be used to make a chat bot or something similar. You can bind either a function or a channel to any or all messages. The Send() method uses AppleScript, which is likely going to require some tinkering. You got this far, so I trust you'll figure that out. Let me know how it works out.
The library uses `fsnotify` to poll for db updates, then checks the database for changes. Only new messages are processed. If somehow `fsnotify` fails it will fall back to polling the database. Pay attention to the debug/error logs.
Index ¶
- Variables
- type Config
- type Incoming
- type Logger
- type Messages
- func (m *Messages) ClearMessages() error
- func (m *Messages) IncomingCall(match string, callback func(msg Incoming))
- func (m *Messages) IncomingChan(match string, channel chan Incoming)
- func (m *Messages) RemoveCall(match string) int
- func (m *Messages) RemoveChan(match string) int
- func (m *Messages) RunAppleScript(id string, scripts []string, retry int) (success bool, errs []error)
- func (m *Messages) Send(msg Outgoing)
- func (m *Messages) SetDebugLogger(logger Logger)
- func (m *Messages) SetErrorLogger(logger Logger)
- func (m *Messages) Start() error
- func (m *Messages) Stop()
- type Outgoing
- type Response
Constants ¶
This section is empty.
Variables ¶
var OSAScriptPath = "/usr/bin/osascript"
OSAScriptPath is the path to the osascript binary. macOS only.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { ClearMsgs bool `xml:"clear_messages" json:"clear_messages" toml:"clear_messages" yaml:"clear_messages"` QueueSize int `xml:"queue_size" json:"queue_size" toml:"queue_size" yaml:"queue_size"` Retries int `xml:"retries" json:"retries" toml:"retries" yaml:"retries"` SQLPath string `xml:"sql_path" json:"sql_path" toml:"sql_path" yaml:"sql_path"` Interval time.Duration `xml:"interval" json:"interval" toml:"interval" yaml:"interval"` }
Config is our input data, data store, and interface to methods. Fill out this struct and pass it into imessage.Init()
type Incoming ¶
type Incoming struct { RowID int64 // RowID is the unique database row id. From string // From is the handle of the user who sent the message. Text string // Text is the body of the message. File bool // File is true if a file is attached. (no way to access it atm) }
Incoming is represents a message from someone. This struct is filled out and sent to incoming callback methods and/or to bound channels.
type Logger ¶
type Logger func(msg string, fmt ...interface{})
Logger is a base type to deal with changing log outs. Pass a matching interface (like log.Printf) to capture messages from the running background go routines.
type Messages ¶
Messages is the interface into this module. Init() returns this struct. All of the important library methods are bound to this type. ErrorLog and DebugLog can be set directly, or use the included methods to set them.
func Init ¶
Init is the primary function to retreive a Message handler. Pass a Config struct in and use the returned Messages struct to send and respond to incoming messages.
func (*Messages) ClearMessages ¶
ClearMessages deletes all conversations in MESSAGES.APP. Use this only if Messages is behaving poorly. Or, never use it at all. This probably doesn't do anything you want to do.
func (*Messages) IncomingCall ¶
IncomingCall connects a callback function to a matched string in a message. This methods creates a callback that is run in a go routine any time a message containing `match` is found. Use '.*' for all messages. Supports regexp.
func (*Messages) IncomingChan ¶
IncomingChan connects a channel to a matched string in a message. Similar to the IncomingCall method, this will send an incoming message to a channel. Any message with text matching `match` is sent. Regexp supported. Use '.*' for all messages. The channel blocks, so avoid long operations.
func (*Messages) RemoveCall ¶
RemoveCall deletes a message match to function callback made with IncomingCall()
func (*Messages) RemoveChan ¶
RemoveChan deletes a message match to channel made with IncomingChan()
func (*Messages) RunAppleScript ¶
func (m *Messages) RunAppleScript(id string, scripts []string, retry int) (success bool, errs []error)
RunAppleScript runs a script on the local system. While not directly related to iMessage and Messages.app, this library uses AppleScript to send messages using imessage. To that end, the method to run scripts is also exposed for convenience.
func (*Messages) Send ¶
Send is the method used to send an iMessage. The messages are queued in a channel and sent 1 at a time with a small delay between. Each message may have a callback attached that is kicked off in a go routine after the message is sent.
func (*Messages) SetDebugLogger ¶
SetDebugLogger allows a library consumer to do whatever they want with the debug logs from this package. Pass in a Logger interface like log.Printf to capture messages created by the go routines. The DebugLog struct item is exported and can also be set directly without a method call.
func (*Messages) SetErrorLogger ¶
SetErrorLogger allows a library consumer to do whatever they want with the error logs from this package. Pass in a Logger interface like log.Printf to capture messages created by the go routines. The ErrorLog struct item is exported and can also be set directly without a method call.
type Outgoing ¶
type Outgoing struct { ID string // ID is only used in logging and in the Response callback. To string // To represents the message recipient. Text string // Text is the body of the message or file path. File bool // If File is true, then Text is assume to be a filepath to send. Call func(*Response) // Call is the function that is run after a message is sent off. }
Outgoing struct is used to send a message to someone. Fll it out and pass it into Messages.Send() to fire off a new iMessage.