瀏覽代碼

[Feature] Neuron Twitter to allow posting a tweet

monf 8 年之前
父節點
當前提交
da567ccfc1
共有 3 個文件被更改,包括 67 次插入5 次删除
  1. 3 1
      brains/twitter.yml
  2. 50 1
      neurons/twitter/README.md
  3. 14 3
      neurons/twitter/Twitter.py

+ 3 - 1
brains/twitter.yml

@@ -1,8 +1,10 @@
 ---
 
-  - name: "post tweet"
+  - name: "post-tweet"
     neurons:
       - twitter:
+          consumer_key: ""
+          consumer_secret: ""
           access_token_key: ""
           access_token_secret: ""
           args:

+ 50 - 1
neurons/twitter/README.md

@@ -1,2 +1,51 @@
-## Twitter 
+# Twitter 
 
+## Synopsis
+
+Twitter is an online social networking service that enables users to send and read short 140-character messages called "tweets".
+
+## Options
+
+| parameter           | required | default | choices | comment                     |
+|---------------------|----------|---------|---------|-----------------------------|
+| consumer_key        | yes      | None    |         | User info                   |
+| consumer_secret     | yes      | None    |         | User info                   |
+| access_token_key    | yes      | None    |         | User info                   |
+| access_token_secret | yes      | None    |         | User info                   |
+| tweet               | yes      | None    |         | The sentence to be tweeted  |
+
+## Return Values
+
+| Name  | Description                     | Type   | sample          |
+|-------|---------------------------------|--------|-----------------|
+| tweet | The tweet which has been posted | string | coucou kalliopé |
+
+## Synapses example
+
+```
+- name: "post-tweet"
+    neurons:
+      - twitter:
+          consumer_key: ""
+          consumer_secret: ""
+          access_token_key: ""
+          access_token_secret: ""
+          args:
+            - tweet
+    signals:
+      - order: "post on Twitter {{ tweet }}"
+```
+
+## Notes
+
+In order to be able to post on Twitter, you need to grant access of your application on Twitter by creating your own app associate to your profile. 
+
+### How to create my Twitter app
+
+1. Sign in your (Twitter account)[www.twitter.com]
+2. Let's create your app (apps.twitter.com)[apps.twitter.com]
+3. click on the button "Create New App"
+4. Fill in your application details
+5. Create your acess token (to post a tweet, you need at least "Read and Write" access)
+6. Get your consumer_key, consumer_secret, access_token_key and access_token_secret from the tab "Key and access token" (Keep them secret !)
+7. Post your first message with this neuron !

+ 14 - 3
neurons/twitter/Twitter.py

@@ -6,13 +6,16 @@ from core.NeuronModule import NeuronModule
 class Twitter(NeuronModule):
     def __init__(self, **kwargs):
 
-        consumer_key = ''
-        consumer_secret = ''
-
+        consumer_key = kwargs.get('consumer_key', None)
+        consumer_secret = kwargs.get('consumer_secret', None)
         access_token_key = kwargs.get('access_token_key', None)
         access_token_secret = kwargs.get('access_token_secret', None)
         tweet = kwargs.get('tweet', None)
 
+        if consumer_key is None:
+            raise NotImplementedError("Twitter needs a consumer_key")
+        if consumer_secret is None:
+            raise NotImplementedError("Twitter needs a consumer_secret")
         if access_token_key is None:
             raise NotImplementedError("Twitter needs an access_token_key")
         if access_token_secret is None:
@@ -27,4 +30,12 @@ class Twitter(NeuronModule):
 
         status = api.PostUpdate(tweet)
 
+        message = {
+            "status" : status
+        }
+
+        self.say(message)
+
+
+