瀏覽代碼

Merge pull request #14 from kalliope-project/neuron-twitter

Neuron twitter
Nicolas Marcq 8 年之前
父節點
當前提交
3987cd41db

+ 1 - 0
Docs/dev_env_install.md

@@ -33,6 +33,7 @@ pip install wakeonlan
 pip install ipaddress
 pip install pyowm
 pip install flask
+pip install python-twitter==3.1
 ```
 
 ### Test your env

+ 13 - 12
Docs/neuron_list.md

@@ -2,16 +2,17 @@
 
 A neuron is a module you can use in your synapses. See the [complete neuron documentation](neurons.md) for more information.
 
-| Name                                               | Description                                                                             |
-|----------------------------------------------------|-----------------------------------------------------------------------------------------|
-| [ansible_task](../neurons/ansible_task/)           | Run an ansible playbook                                                                 |
-| [gmail_checker](../neurons/gmail_checker/)         | Get the number of unread email and their subjects from a gmail account                  |
-| [kill_switch](../neurons/kill_switch/)             | Stop Jarvis process                                                                     |
-| [push_message](../neurons/push_message/)           | Send a push message to a remote device like Android/iOS/Windows Phone or Chrome browser |
-| [say](../neurons/say/)                             | Make Jarvis talk by using TTS                                                           |
-| [script](../neurons/script/)                       | Run an executable script                                                                |
-| [shell](../neurons/command/)                       | Run a shell command                                                                     |
-| [sleep](../neurons/sleep/)                         | Make Jarvis sleep for a while before continuing                                         |
-| [systemdate](../neurons/systemdate/)               | Give the local system date and time                                                     |
-| [tasker_autoremote](../neurons/tasker_autoremote/) | Send a message to Android tasker app                                                    |
+| Name                                               | Description                                                                             | Used to sort      |
+|----------------------------------------------------|-----------------------------------------------------------------------------------------|-------------------|
+| [ansible_task](../neurons/ansible_task/)           | Run an ansible playbook                                                                 | ansible_task      |
+| [gmail_checker](../neurons/gmail_checker/)         | Get the number of unread email and their subjects from a gmail account                  | gmail_checker     |
+| [kill_switch](../neurons/kill_switch/)             | Stop Jarvis process                                                                     | kill_switch       |
+| [push_message](../neurons/push_message/)           | Send a push message to a remote device like Android/iOS/Windows Phone or Chrome browser | push_message      |
+| [say](../neurons/say/)                             | Make Jarvis talk by using TTS                                                           | say               |
+| [script](../neurons/script/)                       | Run an executable script                                                                | script            |
+| [shell](../neurons/command/)                       | Run a shell command                                                                     | shell             |
+| [sleep](../neurons/sleep/)                         | Make Jarvis sleep for a while before continuing                                         | sleep             |
+| [systemdate](../neurons/systemdate/)               | Give the local system date and time                                                     | systemdate        |
+| [tasker_autoremote](../neurons/tasker_autoremote/) | Send a message to Android tasker app                                                    | tasker_autoremote |
+| [twitter](../neurons/twitter/)                     | Send a Twit from kalliope                                                               | twitter           |
 

+ 1 - 0
brain.yml

@@ -11,3 +11,4 @@
   - !include brains/systemdate.yml
   - !include brains/tasker_autoremote.yml
   - !include brains/wake_on_lan.yml
+  - !include brains/twitter.yml

+ 15 - 0
brains/twitter.yml

@@ -0,0 +1,15 @@
+---
+
+  - name: "post-tweet"
+    neurons:
+      - twitter:
+          consumer_key: ""
+          consumer_secret: ""
+          access_token_key: ""
+          access_token_secret: ""
+          say_template:
+          - "voici le tweet {{ tweet }}"
+          args:
+            - tweet
+    signals:
+      - order: "envoie sur Twitter {{ tweet }}"

+ 1 - 0
install/files/python_requirements.txt

@@ -11,4 +11,5 @@ pushetta==1.0.15
 wakeonlan==0.2.2
 ipaddress==1.0.16
 pyowm==2.5.0
+python-twitter==3.1
 flask==0.11.1

+ 1 - 0
neurons/__init__.py

@@ -10,3 +10,4 @@ from push_message import Push_message
 from openweathermap import Openweathermap
 from tasker_autoremote import Tasker_autoremote
 from wake_on_lan import Wake_on_lan
+from twitter import Twitter

+ 51 - 0
neurons/twitter/README.md

@@ -0,0 +1,51 @@
+# Twitter 
+
+## Synopsis
+
+This neuron allows you to send a tweet on your timeline.
+
+## 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](https://www.twitter.com)
+2. Let's create your app [apps.twitter.com](https://apps.twitter.com)
+3. click on the button "Create New App"
+4. Fill in your application details
+5. Create your access 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 !

+ 42 - 0
neurons/twitter/Twitter.py

@@ -0,0 +1,42 @@
+import twitter
+
+from core.NeuronModule import NeuronModule, InvalidParameterException
+
+
+class Twitter(NeuronModule):
+    def __init__(self, **kwargs):
+
+        super(Twitter, self).__init__(**kwargs)
+
+        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 InvalidParameterException("Twitter needs a consumer_key")
+        if consumer_secret is None:
+            raise InvalidParameterException("Twitter needs a consumer_secret")
+        if access_token_key is None:
+            raise InvalidParameterException("Twitter needs an access_token_key")
+        if access_token_secret is None:
+            raise InvalidParameterException("Twitter needs and access_token_secret")
+        if tweet is None:
+            raise InvalidParameterException("You need to provide something to tweet !")
+
+        api = twitter.Api(consumer_key=consumer_key,
+                          consumer_secret=consumer_secret,
+                          access_token_key=access_token_key,
+                          access_token_secret=access_token_secret)
+
+        status = api.PostUpdate(tweet)
+        message = {
+            "tweet" : status.text
+        }
+
+        self.say(message)
+
+
+
+

+ 1 - 0
neurons/twitter/__init__.py

@@ -0,0 +1 @@
+from Twitter import Twitter