Google Hangouts聊天机器人可以通过传入的Webhook发送DM吗? [英] Google Hangouts Chat bot to send DMs via incoming webhook?
问题描述
是否可以设置这个简单的机器人 ,使用传入的Webhook,但以DM(不是@mention)的形式将消息发送给特定用户?
我的猜测不是.但是我怎么能做到这一点呢?
Is it possible to set up this simple bot, using an incoming webhook, but send the message as a DM (not a @mention) to specific user(s)?
My guess is no. But then how could I achieve this?
Right now, the message is just sent into the room in which the bot was added and I can't see anything on DMs in the messaging docs.
当前,您可以通过设置所谓的机器人用户在Slack中非常轻松地实现此目标.并使用他们的 chat.postMessage
,但是我想在Google Hangouts聊天中进行
You can currently achieve this very easily in Slack by setting up a so called bot user and using their chat.postMessage
but I would like to do this in Google Hangouts Chat instead.
from httplib2 import Http
from json import dumps
#
# Hangouts Chat incoming webhook quickstart
#
def main():
url = '<INCOMING-WEBHOOK-URL>'
bot_message = {
'text' : 'Hello from Python script!'}
message_headers = { 'Content-Type': 'application/json; charset=UTF-8'}
http_obj = Http()
response = http_obj.request(
uri=url,
method='POST',
headers=message_headers,
body=dumps(bot_message),
)
print(response)
if __name__ == '__main__':
main()
推荐答案
到目前为止,无论您使用的是Webhook(还是您使用的)还是REST API,您都需要他们的Space ID或webhook url才能私密地向用户DM.除非您具有此空间ID/Webhook,否则您无法向用户发送消息.一种获取方法是向用户询问其spaceID并将其存储.无论哪种方式,Google API都没有提供一种方法来检索与您正在其中谈论的当前ID不同的Space ID.这意味着,漫游器只能在某个时刻向用户发送消息,告知其与其进行交互.
As of now you would need their Space ID or webhook url in order to DM the user privately whether you use a webhook (like you did) or REST API. Unless you have this Space ID/Webhook you cannot message a user. One way of getting it, is to ask the user for their spaceID and store it. Either way, Google API hasn't given a way to retrieve a different Space ID then the current one you are talking inside of. This means, a bot can only message users its interacted with at some point.
当前的Space ID值可以从事件JSON(事件['space'] ['name'])中检索,然后使用messages.create向用户发送新消息
The current Space ID value can be retrieved from the event JSON (event['space']['name']) and then using messages.create to send a new message to the user
service.spaces().messages().create( parent = spaceName, body = response).execute()
或者可以从网址 https://chat.google.com/dm/->空间ID在这里< ---
OR it can be retrieved from the url https://chat.google.com/dm/ --> space ID is here <---
Google尚未发布任何为特定用户生成自己的spaceID的方法.
Google has not released any way of generating your own spaceID for a specific user.
为了获得webhook网址.参见下文:
In order to get the webhook url. See below:
然后将webhook网址复制并粘贴到上面的代码中.
then copy and paste the webhook url into your code above.
注意:如果需要,可以使用Google聊天的常规URL以及上面提到的其空间ID和以下格式的密钥和访问令牌来制造此Webhook URL: https://developers.google.com/identity/protocols/OAuth2
NOTICE: If you need, this webhook url can be manufactured using the usual url for google chat with their space ID as mentioned above and a key and access token in this format: https://chat.googleapis.com/v1/spaces/< space ID >/messages?key=A< key goes here > &token=< access token here > For info on how to get a key and access token, read the documentation provided here: https://developers.google.com/identity/protocols/OAuth2
这篇关于Google Hangouts聊天机器人可以通过传入的Webhook发送DM吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!