用于处理和响应hangouts聊天事件的helper python类
hangouts-helper的Python项目详细描述
Hangouts聊天助手
说明
用于处理和响应挂起聊天事件的帮助程序python类。
安装
安装时使用pip
:
pip install hangouts-helper
消息组件
这个库包含几个组件类,以帮助构建hangouts聊天消息。
Message
Section
Card
CardAction
CardHeader
TextParagraph
KeyValue
Image
ButtonList
ImageButton
TextButton
示例
使用官方Hangouts Chat API文档中的Pizza Bot示例,可以使用上面的组件构造相同的消息。
fromhangouts_helper.messageimport(Message,Card,CardHeader,Section,Image,KeyValue,ButtonList,TextButton)message=Message()message.add_card(Card(CardHeader(title='Pizza Bot Customer Support',subtitle='pizzabot@example.com',image_url='https://goo.gl/aeDtrS'),Section(KeyValue(top_label='Order No.',content='12345'),KeyValue(top_label='Status',content='In Delivery')),Section('Location',Image(image_url='https://maps.googleapis.com/...')),Section(ButtonList(TextButton(text='OPEN ORDER').add_link(url='https://example.com/orders/...')))))
调用message.output()
会生成一个dict
,可以转换为json…非常适合于返回对同步聊天事件的响应,或通过hangouts聊天api发送新消息。
{"cards":[{"header":{"imageUrl":"https://goo.gl/aeDtrS","subtitle":"pizzabot@example.com","title":"Pizza Bot Customer Support"},"sections":[{"widgets":[{"keyValue":{"content":"12345","topLabel":"Order No."}},{"keyValue":{"content":"In Delivery","topLabel":"Status"}}]},{"header":"Location","widgets":[{"image":{"imageUrl":"https://maps.googleapis.com/..."}}]},{"widgets":[{"buttons":[{"textButton":{"onClick":{"openLink":{"url":"https://example.com/orders/..."}},"text":"OPEN ORDER"}}]}]}]}]}
聊天处理程序
该库还包括一个类,用于帮助处理传入的聊天事件。HangoutsChatHandler
中包含的方法对应于您可以预期接收的不同事件类型。每个方法都应该返回一个消息响应。
示例
fromenumimportEnumfromhangouts_helper.handlerimportHangoutsChatHandler,SpaceTypefromhangouts_helper.messageimportMessageclassActionMethod(Enum):BUTTON_CLICKED='BUTTON_CLICKED'classMyHangoutsChatHandler(HangoutsChatHandler):ActionMethod=ActionMethoddefhandle_added_to_space(self,space_type,event):ifspace_type==SpaceType.DM:returnMessage(text="Thanks for DM'ing me!")elifspace_type==SpaceType.ROOM:returnMessage(text="Thanks adding me to your room!")defhandle_message(self,message,event):returnMessage(text="Thanks for your message!")defhandle_card_clicked(self,action_method,action_parameters,event):ifaction_method==ActionMethod.BUTTON_CLICKED:returnMessage(text="I've processed your button click!")defhandle_removed_from_space(self,event):pass
一个flask应用程序可以重复Hangouts聊天事件,它可能看起来像:
fromflaskimportFlask,jsonifyapp=Flask(__name__)@app.route('/')defbot_handler():event=request.jsonhandler=MyHangoutsChatHandler()response_message=handler.handle_event(event)returnjsonify(response_message.output())
待办事项
- 在自述文件中为每个组件类型添加示例
- 记录并添加将onclick事件添加到小部件的示例
- 文档枚举(
SpaceType
,EventType
,ActionMethod
,Icon
,ImageStyle
,ResponseType
) - 记录
HangoutsChatHandler
的使用场景
- 将与hangouts聊天api交互的方法添加到
HangoutsChatHandler