Skip to main content

A MegaHAL bot for Twitter

Project description

TwitterHAL

A MegaHAL Twitter bot in Python.

This project is in alpha, and NOT considered stable in any way.

Live examples (in Swedish): @bibel3000, @trendhal3000

Prerequisites

But all those should be installed automatically by pip or setup.py. (detectlanguage is installed by using pip install twitterhal[detectlanguage].)

Usage

Command line

$ twitterhal
usage: twitterhal [-s SETTINGS_MODULE] [-d] [-m] [-f] [-t]
                  [-r | --chat | --stats | --print-config | --post-random | --mark-mentions-answered]

optional arguments:
  -s SETTINGS_MODULE, --settings SETTINGS_MODULE
                        Python path to settings module. If omitted, we try
                        looking for it in the 'TWITTERHAL_SETTINGS_MODULE'
                        environment variable.
  -d, --debug           More verbose logging output
  -m, --include-mentions
                        Include all mentions in replies (rather than just the
                        handle we're replying to)
  -f, --force           Try and force stuff, even if TwitterHAL doesn't want
                        to
  -t, --test            Test mode; doesn't actually post anything
  -r, --run             Run the bot!
  --chat                Chat with the bot
  --stats               Display some stats
  --print-config        Print current parsed config
  --post-random         Post a new random tweet
  --mark-mentions-answered
                        Fetch the latest mentions and mark them all as
                        answered. Useful if you had to re-init the DB.

twitterhal --run will post random tweets at random_post_times (see below), as well as answering all incoming mentions, all while trying its best not to exceed the Twitter API rate limits.

As a library

from twitterhal import TwitterHAL
with TwitterHAL(screen_name="twitterhal", twitter_kwargs={"consumer_key": "foo", "consumer_secret": "bar"}) as hal:
    for mention in hal.get_new_mentions():
        hal.generate_reply(mention)
    hal.generate_random()
    hal.post_from_queue()

Configuration

Settings are read from a Python module specified in the TWITTERHAL_SETTINGS_MODULE environment variable, or whatever module you supply to the command-line utility via the [-s | --settings] parameter.

Some example settings:

SCREEN_NAME = "my_k3wl_twitter_user"
RANDOM_POST_TIMES = [datetime.time(8), datetime.time(16), datetime.time(22)]
INCLUDE_MENTIONS = True
DETECTLANGUAGE_API_KEY = ""
DATABASE_CLASS = "twitterhal.models.Database"
BANNED_USERS = ["my_other_twitterhal_bot"]

TWITTER_API = {
    "consumer_key": "foo",
    "consumer_secret": "bar",
    "access_token_key": "boo",
    "access_token_secret": "far",
    "timeout": 40,
    "tweet_mode": "extended",
}

MEGAHAL_API = {
    "max_length": twitter.api.CHARACTER_LIMIT,
    "brainfile": "twitterhal-brain",
    "order": megahal.DEFAULT_ORDER,
    "timeout": megahal.DEFAULT_HARD_TIMEOUT,
    "banwords": ["MOST", "COMMON", "WORDS"],
}

TWITTER_API contains keyword arguments for twitter.Api. Read more about it here.

MEGAHAL contains keyword arguments for megahal.Megahal. Consult that module for more info.

RANDOM_POST_TIMES: TwitterHAL will post a randomly generated tweet on those points of (local) time every day. Default: 8:00, 16:00, and 22:00 (that is 8 AM, 4 PM and 10 PM, for those of you stuck in antiquity).

INCLUDE_MENTIONS: if True, TwitterHAL will include all mentions in its replies. That is, not only the @handle of the user who wrote to it, but also every user they mentioned in their tweet. Perhaps you should use this carefully. Anyway, the default is False.

BANNED_USERS: We will never respond to, or mention, these users. Useful if you, for example, run two bots and don't want them to get stuck in an eternal loop responding to each other. (Perhaps, someday, I will figure out a clever way to detect such loops automatically.) NB: Don't include the "@"!

MEGAHAL_API["banwords"]: you may want to set this if your bot will not be speaking English. Pro tip: search for a list of the ~300 most commonly used words in your language, and use those.

Extending

You may extend TwitterHAL's database by subclassing TwitterHAL and adding models.DatabaseItem definitions to its init_db() method. Maybe you want to feed the MegaHAL brain by regularily fetching top tweets for trending topics, and need to keep track of those? I know I do.

By default, the database (which is of type models.Database) will contain:

  • posted_tweets (models.TweetList): List of posted Tweets
  • mentions (models.TweetList): List of tweets that mention us, and whether they have been answered

Tweets are internally stored in models.TweetList, which contains the method only_in_language(). This will filter out all tweets that are probably in the chosen language, with the help of the Language Detection API. Just install the PyPI package detectlanguage, get yourself an API key and feed it to detectlanguage.configuration.api_key (or set it in your settings; see above), and you're all set.

If you extend TwitterHAL with new methods that call the Twitter API, it's recommended you also check TwitterHAL's can_do_request(url), where url is something like /statuses/mentions_timeline (consult this page for full list), to see whether this call should be made at this time.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twitterhal-0.4.3.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

twitterhal-0.4.3-py3-none-any.whl (33.3 kB view details)

Uploaded Python 3

File details

Details for the file twitterhal-0.4.3.tar.gz.

File metadata

  • Download URL: twitterhal-0.4.3.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/2.7.16

File hashes

Hashes for twitterhal-0.4.3.tar.gz
Algorithm Hash digest
SHA256 f802aab7340e3e1ffd9200d4bbf728111338d8043f1344eabcceddabf465a84c
MD5 b89f3b91f24a1ecc1137df5879ea5ba7
BLAKE2b-256 3d6b52477f1f0ddf431914a6f89ab8f7a9d221e50d5f5e1d05d8ec9f92ae2915

See more details on using hashes here.

File details

Details for the file twitterhal-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: twitterhal-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 33.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/2.7.16

File hashes

Hashes for twitterhal-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d40829a02ac60233410015057b3a2a03cc3135413f15bbe9f71ac41b96ba49b4
MD5 045e108ab0bddb215c6ebf238c9e6639
BLAKE2b-256 064172c1d17741578ca63dbc4a6e5a249db39fbb2853d65cbaa2468fd2ffef7f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page