{"id":13880007,"url":"https://github.com/basecamp/cognition","last_synced_at":"2025-07-20T03:31:16.053Z","repository":{"id":27584064,"uuid":"31066837","full_name":"basecamp/cognition","owner":"basecamp","description":"Match text; run commands. Works great for building a chatbot!","archived":false,"fork":false,"pushed_at":"2025-02-04T23:15:35.000Z","size":70,"stargazers_count":46,"open_issues_count":0,"forks_count":6,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-07-10T16:09:30.678Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/basecamp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-02-20T13:51:59.000Z","updated_at":"2025-06-23T13:02:24.000Z","dependencies_parsed_at":"2024-06-02T22:14:28.445Z","dependency_job_id":"0f6ab495-db9b-4464-b624-7dffc740cbe8","html_url":"https://github.com/basecamp/cognition","commit_stats":{"total_commits":65,"total_committers":5,"mean_commits":13.0,"dds":0.09230769230769231,"last_synced_commit":"d0c4385fe3eb8df93b3a328e37e2f87a67ac3f2e"},"previous_names":["anoldguy/cognate"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/basecamp/cognition","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fcognition","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fcognition/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fcognition/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fcognition/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/basecamp","download_url":"https://codeload.github.com/basecamp/cognition/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fcognition/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265524602,"owners_count":23782009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-06T08:02:43.233Z","updated_at":"2025-07-16T16:30:42.531Z","avatar_url":"https://github.com/basecamp.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Cognition\n\nThis is a gem that parses a message, and compares it to various matchers.\nWhen it finds the **first match**, it executes an associated block of code or\nmethod, returning the output of whatever was run.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'cognition'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install cognition\n\n## Usage\n\nInstantiate:\n```ruby\nbot = Cognition::Bot.new\n```\n\nProcess your message:\n```ruby\nresult = bot.process('command I need to process')\n```\n\nYou can also include metadata with your message, like user info, or whatever:\n```ruby\nresult = bot.process('another command', {user_id: 15, name: 'Bob'})\n```\n\nInternally, `Cognition` will turn your values into a `Cognition::Message` so\nthe metadata will be passed along with the message, and arbitrary metadata\nis available in the #metadata Hash:\n```ruby\nmsg = Cognition::Message('another command', {user_id: 15, name: 'Bob'})\nmsg.metadata   # Returns { user_id: 15, name: 'Bob' }\n```\n\n### Special metadata\nIf you include a `callback_url` key in your metadata, we'll give you a\nconvenience method to reply to it using the `reply` method.  This will\ninvoke a HTTParty POST back to the URL with your text sent as the\n`content` variable.\n```ruby\nmsg = Cognition::Message('another command', {\n  callback_url: \"http://foo.bar/baz\",\n  user_id: 15,\n  name: 'Bob'\n})\n\nmsg.reply(\"foo\")   # Posts 'content=foo' to http://foo.bar/baz\n```\n\n## Creating a Plugin\nCreating plugins is easy. Subclass `Cognition::Plugins::Base` and setup your\nmatches and logic that should be run.  Your methods will be passed the `msg`\nobject and any `metadata` you've passed to the bot's `process` method.  Here's\nan example plugin:\n```ruby\nclass Hello \u003c Cognition::Plugins::Base\n  # Simple string based matcher. Must match *EXACTLY*\n  match 'hello', :hello, help: { 'hello' =\u003e 'Returns Hello World' }\n\n  # Advanced Regexp based matcher. Capture groups are made available\n  # via MatchData in the matches method\n  match /hello\\s*(?\u003cname\u003e.*)/, :hello_person, help: {\n    'hello \u003cname\u003e' =\u003e 'Greets you by name!'\n  }\n\n  def hello(*)\n    'Hello World'\n  end\n\n  def hello_person(msg, match_data = nil)\n    name = match_data[:name]\n    \"Hello #{name}\"\n  end\nend\n```\n\nAfter you've done that, you will be able to do:\n```ruby\nbot.register(Hello)\nbot.process(\"help hello\")  # \"hello \u003cname\u003e - Greets you by name!\"\nbot.process(\"hello\")       # \"Hello World\"\nbot.process(\"hello foo\")   # \"Hello foo\"\n```\n\n### Rendering templates\nTemplates are opt-in right now, you need to call `render` yourself, and it\nwill return a string with the rendered contents of a template. What template,\nyou ask? The default for `/path/to/hello.rb` will look for a templates in\n`/path/to/hello/views/`.\n\nGiven the following plugin:\n```ruby\nclass Hello \u003c Cognition::Plugins::Base\n  # ...snipped\n\n  def hello(*)\n    render\n  end\n\n  def hi(*)\n    render(template: \"/path/to/template.html.erb\")\n  end\n\n  def hey(*)\n    render(type: \"text\", extension: \"haml\")\n  end\nend\n```\n\n  1. The `hello` method will look for `/path/to/hello/views/hello.html.erb`\n  2. The `hi` method will look for `/path/to/template.html.erb`\n  3. The `hey` method will look for `/path/to/hello/views/hey.text.haml`\n\nSetting instance variables or passing locals is up to the plugin creator.\nThe `render` method takes a hash with the following keys:\n```ruby\n{\n  template: \"full/path/to/template/file\", # FULL path to template file\n  type: \"type of response\"                # text, html, json, etc...\n  extension: \"engine file extension\"      # erb, haml, etc...\n  locals: {x: \"foo\", y: \"bar\"}            # local variables, access as x \u0026 y\n}\n```\n\n## Contributing\n\n1. Fork it ( https://github.com/anoldguy/cognition/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasecamp%2Fcognition","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbasecamp%2Fcognition","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasecamp%2Fcognition/lists"}