内部通信API包装

python-intercom的Python项目详细描述


PyPI VersionPyPI DownloadsTravis CI BuildCoverage Status

内部通信api的python绑定(https://api.intercom.io)。

API Documentation

Package Documentation

升级信息

python内部通信的版本3与不向后兼容 以前的版本。

版本3不再使用全局设置方法,而是使用 对讲机客户端。

安装

pip install python-intercom

基本用法

配置客户端

fromintercom.clientimportClientintercom=Client(personal_access_token='my_personal_access_token')

请注意,某些资源将需要扩展作用域访问令牌:Setting up Personal Access Tokens

资源

此API支持的资源:

https://api.intercom.io/users
https://api.intercom.io/contacts
https://api.intercom.io/companies
https://api.intercom.io/counts
https://api.intercom.io/tags
https://api.intercom.io/notes
https://api.intercom.io/segments
https://api.intercom.io/events
https://api.intercom.io/conversations
https://api.intercom.io/messages
https://api.intercom.io/subscriptions
https://api.intercom.io/jobs
https://api.intercom.io/bulk

示例

用户

# Find user by emailuser=intercom.users.find(email="bob@example.com")# Find user by user_iduser=intercom.users.find(user_id="1")# Find user by iduser=intercom.users.find(id="1")# Create a useruser=intercom.users.create(email="bob@example.com",name="Bob Smith")# Delete a useruser=intercom.users.find(id="1")deleted_user=intercom.users.delete(user)# Update custom_attributes for a useruser.custom_attributes["average_monthly_spend"]=1234.56intercom.users.save(user)# Perform incrementinguser.increment('karma')intercom.users.save(user)# Iterate over all usersforuserinintercom.users.all():...# Bulk operations.# Submit bulk job, to create users, if any of the items in create_items match an existing user that user will be updatedintercom.users.submit_bulk_job(create_items=[{'user_id':25,'email':'alice@example.com'},{'user_id':25,'email':'bob@example.com'}])# Submit bulk job, to delete usersintercom.users.submit_bulk_job(delete_items=[{'user_id':25,'email':'alice@example.com'},{'user_id':25,'email':'bob@example.com'}])# Submit bulk job, to add items to existing jobintercom.users.submit_bulk_job(create_items=[{'user_id':25,'email':'alice@example.com'}],delete_items=[{'user_id':25,'email':'bob@example.com'}],'job_id':'job_abcd1234')

管理员

# Iterate over all adminsforadmininintercom.admins.all():...

公司
# Add a user to one or more companiesuser=intercom.users.find(email='bob@example.com')user.companies=[{'company_id':6,'name':'Intercom'},{'company_id':9,'name':'Test Company'}]intercom.users.save(user)# You can also pass custom attributes within a company as you do thisuser.companies=[{'id':6,'name':'Intercom','custom_attributes':{'referral_source':'Google'}}]intercom.users.save(user)# Find a company by company_idcompany=intercom.companies.find(company_id='44')# Find a company by namecompany=intercom.companies.find(name='Some company')# Find a company by idcompany=intercom.companies.find(id='41e66f0313708347cb0000d0')# Update a companycompany.name='Updated company name'intercom.companies.save(company)# Iterate over all companiesforcompanyinintercom.companies.all():...# Get a list of users in a companyintercom.companies.users(company.id)

标记

# Tag userstag=intercom.tags.tag_users(name='blue',users=[{'email':'test1@example.com'}])# Untag usersintercom.tags.untag_users(name='blue',users=[{'user_id':'42ea2f1b93891f6a99000427'}])# Iterate over all tagsfortaginintercom.tags.all():...# Tag companiestag=intercom.tags.tag(name='blue',companies=[{'id':'42ea2f1b93891f6a99000427'}])

# Find a segmentsegment=intercom.segments.find(id=segment_id)# Iterate over all segmentsforsegmentinintercom.segments.all():...

注释

# Find a note by idnote=intercom.notes.find(id=note)# Create a note for a usernote=intercom.notes.create(body="<p>Text for the note</p>",email='joe@example.com')# Iterate over all notes for a user via their email addressfornoteinintercom.notes.find_all(email='joe@example.com'):...# Iterate over all notes for a user via their user_idfornoteinintercom.notes.find_all(user_id='123'):...

对话

# FINDING CONVERSATIONS FOR AN ADMIN# Iterate over all conversations (open and closed) assigned to an adminforconvoinintercom.conversations.find_all(type='admin',id='7'):...# Iterate over all open conversations assigned to an adminforconvoinintercom.conversations.find_all(type='admin',id=7,open=True):...# Iterate over closed conversations assigned to an adminforconvointercom.conversations.find_all(type='admin',id=7,open=False):...# Iterate over closed conversations for assigned an admin, before a certain# moment in timeforconvoinintercom.conversations.find_all(type='admin',id=7,open=False,before=1374844930):...# FINDING CONVERSATIONS FOR A USER# Iterate over all conversations (read + unread, correct) with a user based on# the users emailforconvoinintercom.onversations.find_all(email='joe@example.com',type='user'):...# Iterate over through all conversations (read + unread) with a user based on# the users emailforconvoinintercom.conversations.find_all(email='joe@example.com',type='user',unread=False):...# Iterate over all unread conversations with a user based on the users emailforconvoinintercom.conversations.find_all(email='joe@example.com',type='user',unread=true):...# FINDING A SINGLE CONVERSATIONconversation=intercom.conversations.find(id='1')# INTERACTING WITH THE PARTS OF A CONVERSATION# Getting the subject of a part (only applies to email-based conversations)conversation.rendered_message.subject# Get the part_type of the first partconversation.conversation_parts[0].part_type# Get the body of the second partconversation.conversation_parts[1].body# REPLYING TO CONVERSATIONS# User (identified by email) replies with a commentintercom.conversations.reply(type='user',email='joe@example.com',message_type='comment',body='foo')# Admin (identified by email) replies with a commentintercom.conversations.reply(type='admin',email='bob@example.com',message_type='comment',body='bar')# User (identified by email) replies with a comment and attachmentintercom.conversations.reply(id=conversation.id,type='user',email='joe@example.com',message_type='comment',body='foo',attachment_urls=['http://www.example.com/attachment.jpg'])# Openintercom.conversations.open(id=conversation.id,admin_id='123')# Closeintercom.conversations.close(id=conversation.id,admin_id='123')# Assignintercom.conversations.assign(id=conversation.id,admin_id='123',assignee_id='124')# Reply and Openintercom.conversations.reply(id=conversation.id,type='admin',admin_id='123',message_type='open',body='bar')# Reply and Closeintercom.conversations.reply(id=conversation.id,type='admin',admin_id='123',message_type='close',body='bar')# ASSIGNING CONVERSATIONS TO ADMINSintercom.conversations.reply(id=conversation.id,type='admin',assignee_id=assignee_admin.id,admin_id=admin.id,message_type='assignment')# MARKING A CONVERSATION AS READintercom.conversations.mark_read(conversation.id)

完全加载嵌入式实体

# Given a conversation with a partial user, load the full user. This can be# done for any entityintercom.users.load(conversation.user)

发送消息
# InApp message from admin to userintercom.messages.create(**{"message_type":"inapp","body":"What's up :)","from":{"type":"admin","id":"1234"},"to":{"type":"user","id":"5678"}})# Email message from admin to userintercom.messages.create(**{"message_type":"email","subject":"Hey there","body":"What's up :)","template":"plain",# or "personal","from":{"type":"admin","id":"1234"},"to":{"type":"user","id":"536e564f316c83104c000020"}})# Message from a userintercom.messages.create(**{"from":{"type":"user","id":"536e564f316c83104c000020"},"body":"halp"})# Message from admin to contactintercom.messages.create(**{'body':'How can I help :)','from':{'type':'admin','id':'1234'},'to':{'type':'contact','id':'536e5643as316c83104c400671'}})# Message from a contactintercom.messages.create(**{'from'=>{'type':'contact','id':'536e5643as316c83104c400671'},'body':'halp'})

事件

intercom.events.create(event_name='invited-friend',created_at=time.mktime(),email=user.email,metadata={'invitee_email':'pi@example.org','invite_code':'ADDAFRIEND','found_date':12909364407})# Retrieve event list for user with id:'123abc'intercom.events.find_all(type='user',"intercom_user_id"="123abc)

元数据对象支持一些简单的类型,内部通信可以在这些类型上显示 代表您

intercom.events.create(event_name="placed-order",email=current_user.email,created_at=1403001013metadata={'order_date':time.mktime(),'stripe_invoice':'inv_3434343434','order_number':{'value':'3434-3434','url':'https://example.org/orders/3434-3434'},'price':{'currency':'usd','amount':2999}})

示例中的元数据键值按如下方式处理-

  • 订单日期:日期(键以''u date'结尾)。
  • 条纹发票:条纹发票的标识符(具有 “条纹发票”键)
  • 订单号:丰富的链接(值包含“url”和“value”键)
  • 价格:美元金额(价值包含“金额”和 “货币”键)

批量操作。

# Submit bulk job, to create eventsintercom.events.submit_bulk_job(create_items:[{'event_name':'ordered-item','created_at':1438944980,'user_id':'314159','metadata':{'order_date':1438944980,'stripe_invoice':'inv_3434343434'}},{'event_name':'invited-friend','created_at':1438944979,'user_id':'314159','metadata':{'invitee_email':'pi@example.org','invite_code':'ADDAFRIEND'}}])# Submit bulk job, to add items to existing jobintercom.events.submit_bulk_job(create_items=[{'event_name':'ordered-item','created_at':1438944980,'user_id':'314159','metadata':{'order_date':1438944980,'stripe_invoice':'inv_3434343434'}},{'event_name':'invited-friend','created_at':1438944979,'user_id':"314159",'metadata':{'invitee_email':'pi@example.org','invite_code':'ADDAFRIEND'}}],job_id='job_abcd1234')

联系人

联系人表示应用程序的已注销用户。

# Create a contactcontact=intercom.contacts.create(email="some_contact@example.com")# Update a contactcontact.custom_attributes['foo']='bar'intercom.contacts.save(contact)# Find contacts by emailcontacts=intercom.contacts.find_all(email="some_contact@example.com")# Convert a contact into a userintercom.contacts.convert(contact,user)# Delete a contactintercom.contacts.delete(contact)

计数

# App-wide countsintercom.counts.for_app()# Users in segment countsintercom.counts.for_type(type='user',count='segment')

订阅

订阅对讲机中的事件以接收webhook。

# create a subscriptionintercom.subscriptions.create(url='http://example.com',topics=['user.created'])# fetch a subscriptionintercom.subscriptions.find(id='nsub_123456789')# list subscriptionsintercom.subscriptions.all():...

批量作业

# fetch a jobintercom.jobs.find(id='job_abcd1234')# fetch a job's error feedintercom.jobs.errors(id='job_abcd1234')

错误

您不需要处理来自api调用的http响应 直接的。如果响应不成功,则错误为 将引发intercom.Error的子类。如果需要,你可以 在Error的http代码中,通过它的http_code方法。

下面列出了不同错误子类的列表。就像他们一样 继承IntercomError除了^{tt4}之外,您可以选择$ 或者更具体的错误子类:

AuthenticationErrorServerErrorServiceUnavailableErrorServiceConnectionErrorResourceNotFoundBadGatewayErrorBadRequestErrorRateLimitExceededMultipleMatchingUsersErrorHttpErrorUnexpectedError

速率限制

调用客户机rate_limit_details将返回一个dict,其中包含 有关应用程序当前速率限制的详细信息。

intercom.rate_limit_details# {'limit': 180, 'remaining': 179, 'reset_at': datetime.datetime(2014, 10, 07, 14, 58)}

运行测试

单元测试:

nosetests tests/unit

集成测试:

INTERCOM_PERSONAL_ACCESS_TOKEN=xxx nosetests tests/integration

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java为什么这个方法会重复它的循环?   swing如何在Java中拖放鼠标时获取鼠标信息?   java如何处理异常并向SSE客户端发送错误   java在一个我一直试图制作的简单计算器上遇到了问题   java如何使用黄瓜。类和参数化。在同一个跑步者级别上   测试并发性的javajunit   java致命错误:jenkins的身份验证失败   只要我修改服务器,java SSE客户端就会停止工作(服务器发送事件)   java通过JSP获取JSON到JS   java在3d应用程序中集成卫星图像或地图   如何为Java8语言环境实现自己的自定义国家名称列表   java SonarLint | SonarQube批量创建@SuppressWarnings   java删除填充矩形   java hibernate一对多出错,外键错误   java如何获取图像是否被触摸而不是透明背景?LIBGDX   JAVA网SocketException:使用Tomcat重置连接   如何使用java检查文件是SSL证书还是常规文件?   java如何在安卓中使用类似C#等的文件选择器或openfiledialog打开文本文件   java非静态createnewfrom方法?   类在java中将具有多个变量类型的对象传递到arraylist时遇到问题