Coder Social home page Coder Social logo

pyflock's Introduction

pyflock

pyflock is a python client for FlockOS. pyflock currently runs only on Python 2.x

Install

If you are using virtualenv, use the following command to create the virtualenv:

virtualenv venv -p python2

Install pyflock using:

pip install git+git://github.com/flockchat/pyflock

Usage

Setup

Import the various classes and methods needed

from pyflock import FlockClient, verify_event_token
from pyflock import Message, SendAs, Attachment, Views, WidgetView, HtmlView, ImageView, Image, Download, Button, OpenWidgetAction, OpenBrowserAction, SendToAppAction

Create the flock client (needed for all apis except verify_event_token

flock_client = FlockClient(token=bot_token, app_id=app_id) # 

token can be an user token, For group and user apis, user token is required. For sending messages it can either be an user token or bot token. to in message apis can be either user or group ids.

Index

Verifying event tokens

See Event Tokens Docs

event_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBJZCI6Im15LWFwcCIsInVzZXJJZCI6InU6M2QwMDQzMDItYTk3ZC00MDE2LTkxYjQtNmMyMjFiYjQ3ODFkIiwiZXhwIjoyMDAwMDAwMDAwLCJpYXQiOjE0Njk1NDE1NzIsImp0aSI6IjU2OGVhZGY4LTc3ZmMtNDEwOC05MWRhLWQ5NGRhNDZkNzA5YiJ9.-_lhKHsGE_s9a4apLYPgLVWW2UZtD4_-B8yxKtAmmqg'
app_secret = '869eb1d0-419d-4747-98b4-6d81360a6681'
print verify_event_token(event_token = event_token, app_secret = app_secret)

Sending messages

Send a simple message

simple_message = Message(to=user_guid,text="Hello, world")
# returns a message id
res = flock_client.send_chat(simple_message)
print(res)

Send a message using a custom name and profile image

send_as_hal = SendAs(name='HAL-9000', profile_image='https://pbs.twimg.com/profile_images/1788506913/HAL-MC2_400x400.png')
send_as_message = Message(to=user_guid,text="I'm sorry Dave, I'm afraid I can't do that",send_as=send_as_hal)
res = flock_client.send_chat(send_as_message)
print(res)

Send a widget view

views = Views()
widget = WidgetView(src="http://example.com",height=250)
views.add_widget(widget)

attachment = Attachment(title="Test widget", description="Replace src with your own page", views=views)
# NOTE: attachments is an array of attachment
widget_message = Message(to=user_guid, attachments = [attachment])
res = flock_client.send_chat(widget_message)
print(res)

Send a HTML view

views = Views()
html = HtmlView(inline="It <b>Works</b>",height=50)
views.add_html(html)

attachment = Attachment(title="Test html", description="Replace inline with your own html", views=views)
# NOTE: attachments is an array of attachment
html_message = Message(to=user_guid, attachments = [attachment])
res = flock_client.send_chat(html_message)
print(res)

Send a FlockML view

# NOTE: No need for a flockml view object
views = Views()
views.add_flockml("<flockml>FlockML is <b>AWESOME</b></flockml>")

attachment = Attachment(title="Test flockml", description="Replace flockml with your own flockml", views=views)
# NOTE: attachments is an array of attachment
flockml_message = Message(to=user_guid, attachments = [attachment])
res = flock_client.send_chat(flockml_message)
print(res)

Send a Image view

views = Views()
image = ImageView(original=Image(src="http://library.acropolis.org/wp-content/uploads/2014/11/One_ring.png", width=400, height=400),filename="onering.png")
views.add_image(image)

attachment = Attachment(title="Test image", description="One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them", views=views)
# NOTE: attachments is an array of attachment
image_message = Message(to=user_guid, attachments = [attachment])
res = flock_client.send_chat(image_message)
print(res)

Send download files

d = Download(src="http://wallpapercave.com/wp/H630T6R.jpg")

views = Views()
views.add_flockml("<flockml>Download the <i>matrix</i></flockml>")

# NOTE: downloads is always a list
attachment = Attachment(title="Test files", downloads=[d], views=views)
files_message = Message(to=user_guid, attachments = [attachment])
res = flock_client.send_chat(files_message)
print(res)

Button with openwidget, open url & send to app service

b1 = Button(name = "Harry Potter", id="harry", action=OpenWidgetAction(url="https://goo.gl/aygRGf", desktop_type="sidebar"))
b2 = Button(name = "Ron Weasley", id="ron", action=OpenBrowserAction(url="https://goo.gl/gDpMVn", send_context=True))
b3 = Button(name = "Hermione Granger", id="hermione", action=SendToAppAction())
attachment = Attachment(title="Test buttons", buttons=[b1,b2,b3])
button_message = Message(to=user_guid, text="Who is your favourite Harry Potter character?", attachments = [attachment])
res = flock_client.send_chat(button_message)
print(res)

Now, just for fun, let us change colours

attachment = Attachment(title="Test colour", color="#FF0000", description="It is red!")
color_message = Message(to=user_guid, attachments=[attachment])
res = flock_client.send_chat(color_message)
print(res)

Group APIs

Get group info

print flock_client.get_group_info(group_id)

Get group members

print flock_client.get_group_members(group_id)

Get groups list of which user is member of

print flock_client.get_groups()

User APIs

Get user info

print flock_client.get_user_info()

Get all contacts

print flock_client.get_contacts()

pyflock's People

Contributors

balajiganapathi avatar

Watchers

James Cloos avatar Arvind Mishra avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.