Post Twitter status updates and send DMs
The f451 Communications module allows you to post Twitter status updates and send DMs to a given recipient using a unified interface. In it’s simplest for form, you can post plain text message status update. But you can also send DMs to specific recipients.
In order to use this service, you’ll need to set up an account with Twitter. Once that is done, you get your API key and other relevant account credentials.
Note
Please refer to the “Configuration files” section for more detailed information on configuration requirements for Twitter status updates and DMs.
Core features
Post status updates – Status updates can be text-only as well as include images.
Send DMs to multiple recipients – Allows you to send Twitter DM messages to multiple recipients
Post status updates using the main Comms object
You can send Twitter status updates using the unified send_message() method of the Comms object as follows:
STEP 1: initialize Comms object
comms = Comms(
secrets, # credentials required for services
config # optional settings
)
STEP 2: call send_message() method
comms.send_message(
"Hello world!", # plain text message
attribs # misc. attributes required to send message
)
ALTERNATIVE: call send_message_via_twitter() method
comms.send_message_via_twitter(
"Hello world!", # plain text message
attribs # misc. attributes required to send message
)
Note
Please to section “Configuration files” for more details on defining secrets and config data.
Note
Please see below for a detailed description of options for attribs and note that some items are required in order to send emails. Also note that some values can be stored as default values when initializing the main Comms object and the do not need to be included in the attribs argument.
Note
If alternative send_message_via_twitter() is used, then channels attribute is not required in the attribs argument.
Post status updates using the comms_twitter.Twitter object
You can send SMS using the send_message() method of the Twitter object in the comms_twitter sub-module as follows:
Initialize the
Twitterobject:
client = Twitter(
usrKey, # your Twitter user key
usrSecret, # your Twitter user secret
authToken, # your Twitter auth/access token
authSecret, # your Twitter auth/access token secret
defaults # optional default values
)
Call the
send_message()method:
client.send_message(
"Hello world!", # plain text message
attribs # misc. attributes required to send message
)
Note
Please to section “Configuration files” for more details on defining secrets and config data.
Note
Please see below for a detailed description of options for attribs and note that some items are required in order to send Twitter status updates or DMs. Also note that some values can be stored as default values when initializing the main Comms object and the do not need to be included in the attribs argument.
Keywords and values for attribs argument
The following keywords and values are primarily used to pass additional arguments via the **kwargs parameter to the various send_message_<xxxx> functions. Please note that some keywords can also be used for defining API credentials and various default values for use in config files (e.g. config.ini).
account_sid- Required for:
SMS (Twilio) – Twilio account SID as
str
- Example .ini entry:
acct_sid = XX0x00x0x0xxxxx000x0x0x0x000xxx00x
Warning
Do NOT store this value in source code!
app_token- Required for:
Slack – Slack app token as
str
- Example .ini entry:
app_token = xapp-0-X00000000-000000000-00000000xxxxx00000000xxxxx00000000
Warning
Do NOT store this value in source code!
auth_secret- Required for:
Twitter – Twitter auth secret as
str
- Example .ini entry:
auth_secret = xx0000000000xx000000x0000000x0xx00000xxx00000
Warning
Do NOT store this value in source code!
bcc_email- Optional for:
Email (Mailgun) – see
to_emailattribute
cc_email- Optional for:
Email (Mailgun) – see
to_emailattribute
channels- Required for:
Comms.send_message()method – one or more tags asstrorlistofstr
- Examples:
Send message to all available channels:
{"channels": "all"}Send message to Twitter and Slack:
{"channels": ["f451_twitter", "f451_slack"]}or{"channels": "f451_twitter|f451_slack"}
Note
Use
channel_mapattribute to map custom keywords against channel names.
channel_mapOptional for:
mainsection- Examples:
Map email and sms keywords:
channel_map = email:f451_mailgun|sms:f451_twilio
debugReserved
file_title- Optional for:
Slack – file title as
str
- Examples:
Simple title:
{"file_title": "List of Gotham City crime bosses"}
fromOptional for:
mainsection. Complete sender info (e.g. name, phone, email, etc.) asstrorlistofstr- Examples:
Complete sender info:
from = name:Batman|email:batman@example.com|phone:+12125550001|slack:Batman|twitter:Batman
Note
Use
fromattribute instead of individual attributes (e.gfrom_name,from_email,from_phone, etc.).
from_domain- Required for:
Email (Mailgun) – Mailgun domain as
str
- Example .ini entry:
from_domain = xxxxxxx00000000000000000000.mailgun.org
Warning
Do NOT store this value in source code!
from_emailReserved
from_name- Optional for:
Email (Mailgun) – sender name
str
from_phone- Required for:
SMS (Twilio) – Twilio sender phone number as
str
- Example .ini entry:
from_phone = +12125150000
Warning
Do NOT store this value in source code!
from_slack- Optional for:
Slack – sender name
str
from_twitter- Required for:
Twitter DMs – sender name
str
html- Optional for:
Email (Mailgun) – HTML version of email message as
str
- Example:
Simple:
{"html": "<html>Hello world!</html>"}
icon_emoji- Optional for:
Slack – a
strthat represents the emoji short code.
- Examples:
Use See No Evil emoji:
{"icon_emoji": ":see_no_evil:"}
inline- Optional for:
Email (Mailgun) – one or more file names as
strorlistofstr
Valid formats: .png, .jpg, .gif
- Examples:
Single inline image:
{"inline": path/to/image1.jpg"}Multiple inline images:
{"inline": ["path/to/image1.jpg", "path/to/image2.jpg"]}
log_level- Optional for:
all channels –
intorstr
Default:
logging.INFO- Valid options:
-1orOFF– no loggingintfrom0to100– log level is set to this value.standard log levels defined in Python ‘logging’ package package as
logging.<CONST>or equivalent string name
- Examples:
Enable logging:
{"log_level": logging.INFO}or {“log_level”: “INFO”}Disable logging:
{"log_level": -1}or{"log_level": "OFF"}
mediaValid formats: .png, .jpg, .gif
- Examples:
Single attachment:
{"attachments": path/to/file1.txt"}Multiple attachments:
{"attachments": ["path/to/file1.txt", "path/to/file2.txt"]}
method_updateReserved
method_dm- Optional for:
Twitter – boolean flag. If
Truemessage is sent as DM. This also requires at least one name listed into_twitterattribute.
Default:
False- Examples:
Send message as DM:
{"method_dm": True}
nameReserved
phoneReserved
priv_api_key- Required for:
Email (Mailgun) – Mailgun private API key as
str
- Example .ini entry:
priv_api_key = key-00000000000000000000000000000000
Warning
Do NOT store this value in source code!
publ_val_key- Required for:
Email (Mailgun) – Mailgun public API key as
str
- Example .ini entry:
publ_val_key = pubkey-00000000000000000000000000000000
Warning
Do NOT store this value in source code!
recipientReserved
recipient_data- Optional for:
Email (Mailgun) – list of additional recipient info for batch emails as
struct
- Example:
Email address used as key:
{"recipient_data": {"batman@example.com": {"name":"Batman", "lucky": 13}, ...}}
signing_secret- Required for:
Slack – signing secret as
str
- Example .ini entry:
signing_secret = xxxxx0000000000xxx000000000xxxx00000
Warning
Do NOT store this value in source code!
slackReserved
subject- Required for:
Email (Mailgun) – email subject line as
str
- Examples:
Simple:
{"subject": "Hello world!"}Personalized using
recipientsinfo:{"subject": "Hello %recipients.name%"}
suppress_errors- Optional for:
Email (Mailgun) – boolean flag. If
TrueMailgun exceptions are suppressed.SMS (Twilio) – boolean flag. If
TrueTwilio exceptions are suppressed.Twitter DM – boolean flag. If
TrueTwitter exceptions are suppressed.
Default:
False- Examples:
Suppress errors:
{"suppress_error": True}
tags- Optional for:
Email (Mailgun) – one or more (max 3) tags as
strorlistofstr
- Examples:
Single tag:
{"tags": "greeting"}Multiple tags:
{"tags": ["greeting", "salutation"]}or{"tags": "greeting|salutation"}
testmode- Optional for:
Email (Mailgun) – boolean flag. If
Truetest mode is enabled.
Default:
False- Examples:
Enable test mode:
{"testmode": True}
toOptional for:
mainsection. Complete recipient info (e.g. name, phone, email, etc.) asstrorlistofstr- Examples:
Complete recipient info:
from = name:Batman|email:batman@example.com|phone:+12125550001|slack:Batman|twitter:Batman
Note
Use
toattribute instead of individual attributes (e.gto_name,to_email,to_phone, etc.).
to_channel- Required for:
Slack – one or more Slack channels as
strorlistofstr
- Examples:
Single channel:
{"to_channel": "#GothamCrime"}Multiple recipients:
{"to_channel": ["#GothamCrime", "#NewVillains"]}or{"to_channel": "GothamCrime|NewVillains"}
to_email- Required for:
Email (Mailgun) – one or more email addresses as
strorlistofstr
- Examples:
Single recipient:
{"to_email": "batman@example.com"}Multiple recipients:
{"to_email": ["batman@example.com", "robin@example.com"]}or{"to_email": "batman@example.com|robin@example.com"}
Note
If you send an email to multiple recipients, then also use the
recipient_dataattribute for additional recipient info.
to_phone- Required for:
SMS (Twilio) – one or more phone numbers as
strorlistofstr
- Examples:
Single recipient:
{"to_phone": "+12125550000"}Multiple recipients:
{"to_phone": ["+12125550000", "+12125551111"]}or{"to_phone": "+12125550000|+12125551111"}
to_slack- Optional for:
Slack – one or more Slack user Names as
strorlistofstr. Names listed here will be included with ‘@’ symbol in the beginning of the Slack message.
- Examples:
Single name:
{"to_slack": "batman"}Multiple names:
{"to_slack": ["batman", "robin"]}or{"to_slack": "batman|robin"}
to_twitter- Optional for:
Twitter – one or more Twitter user names as
strorlistofstr. Ifmethod_dmisFalse, then names listed here will be included with ‘@’ symbol in the beginning of the Twitter status update message.
- Examples:
Single name:
{"to_twitter": "batman"}Multiple names:
{"to_twitter": ["batman", "robin"]}or{"to_twitter": "batman|robin"}
tracking- Optional for:
Email (Mailgun) – boolean flag. If
Truetracking is enabled.
Default:
False- Examples:
Enable tracking:
{"tracking": True}
twitterReserved
user_key- Required for:
Twitter – Twitter user key as
str
- Example .ini entry:
user_key = xxxxx0000000000xxx000000000xxxx00000
Warning
Do NOT store this value in source code!
user_secret- Required for:
Twitter – Twitter user secret as
str
- Example .ini entry:
user_secret = xxxxx0000000000xxx000000000xxxx00000xxxxx00000xxxxx0000
Warning
Do NOT store this value in source code!
webhook_sign_key- Required for:
Email (Mailgun) – Mailgun webhook sign key as
str
- Example .ini entry:
webhook_sign_key = key-xxxxx0000000000xxx000000000
Warning
Do NOT store this value in source code!
Note
Attributes that support str and list of str can process the lists either as a string using pipe character (|) as delimiter between values, or as a true lst of str:
list of values as simple
str:"apple|banana|orange"list of values as
lstofstr:["apple", "banana", "orange"]
Examples
Example – structure for simple Twitter status update:
attribs = {
"channels": "f451_twitter",
}
Example – structure for simple Twitter DM:
attribs = {
"channels": "f451_twitter",
"to_twitter": "batman",
"method_dm": True,
}
Example – structure for simple Twitter status update with ‘@’ some Twitter users:
attribs = {
"channels": "f451_twitter",
"to_twitter": ["batman", "robin"],
"method_dm": False,
}