python - 如何使用 python 和 bot 框架创建一个简单的 Skype 机器人

标签 python botframework skype

我正在尝试使用 python 使用 botframework 在 Skype 中创建一个非常简单的机器人。
这是我做的步骤。

  • 在资源中创建了一个 channel 机器人。

  • enter image description here
  • 使用 flask 创建了一个简单的 python web 服务来接收来自机器人的消息。通过 ngrok 公开它并在消息传递端点中添加相同的内容。

  • enter image description here
  • 我从网络聊天中测试了机器人,我在我的 python 网络服务中得到了一个 json
    {
    "recipient": {
    "id": "txBot@b0X6R31x6uQ", 
    "name": "txBot"
     }, 
    "from": {
    "id": "1xFUIEqdQfv", 
    "name": "You"
     }, 
     "entities": [
        {
        "supportsTts": true, 
        "supportsListening": true, 
        "type": "ClientCapabilities", 
        "requiresBotState": true
        }
     ], 
    "locale": "en", 
    "timestamp": "2018-11-22T13:00:42.9086958Z", 
    "channelId": "webchat", 
    "channelData": {
    "clientActivityId": "1542891640077.5912976099256072.0"
    }, 
    "conversation": {
        "id": "b5b52b9e464b4e958b1219dadedfffce"
    }, 
    "serviceUrl": "https://webchat.botframework.com/", 
    "text": "hello from test", 
    "textFormat": "plain", 
    "type": "message", 
    "id": "b5b52b9e464b4e958b1219dadedfffce|0000002"
    }
    
  • 我能够处理这个 json,并且能够在这个例子中让用户输入“来自测试的你好”到我的 web 服务。

  • 我想要做的是将相同的内容从 python 返回给我的网络聊天机器人。

    我也引用了下面的教程。我可以回复回复,但我没有在机器人中得到回复。

    后端的python代码如下
    import urllib
    import json
    
    import requests
    import urllib2, json
    from flask import Flask
    from flask import request
    from flask import make_response
    
    # Flask app should start in global layout
    app = Flask(__name__)
    
    
    @app.route('/webhook', methods=['POST'])
    def webhook():
        print "came inside"
        req = request.get_json(silent=True, force=True)
        print("Request:")
        print(json.dumps(req, indent=4))
        service_url=""
        if req['type']=='conversationUpdate':
                return ''
    
        else:
                print "got input as : ",str(req['text'])
                payload = build_text_message_payload(req, req['text'])
                print "---- calling service url ----"
                #service_url = build_service_url2(req['serviceUrl'],req['conversation']['id'],req['id'])
                service_url = build_service_url2(req['serviceUrl'],req['conversation']['id'])
    
    
        print "---- Payload ----"
        print payload
        # where we are going to send our request
        print "---- Service url ----"
        print service_url
        # let's send the message
        response = send_to_conversation(service_url, payload)
    
        print "****************"
        print response
        # always return a response
    
        #print get_auth_token()
    
        return "success"
    
    
    
    def build_text_message_payload(data, text):
        """Creates a text only message dict"""
        payload = {
        'type': 'message/text',
        'from': {
            'id': data['recipient']['id'],
            'name': data['recipient']['name'],
        },
        'recipient': {
            'id': data['from']['id'],
    
            'name': data['from']['name'],
        },
        'text': text,
    }
        return payload
    
    
    
    
    
    
    def build_service_url2(service_url, cid):
        """build the service url"""
        service_url = '{0}v3/conversations/{1}/activities'.format(
        service_url,
        cid
    )
        return service_url
    
    def send_to_conversation(service_url, payload):
    
      url="https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
      headers1={'Content-Type':'application/x-www-form-urlencoded'}
    
      data=urllib.urlencode({'grant_type':'client_credentials','client_id':'jhxzxk1b2-1jjk-44b4-88bd-566d09crs4sg','client_secret':'lpfgrpDBPxjsLLFC41}@','scope':'https://api.botframework.com/.default'}
      req = urllib2.Request(url, headers=headers1, data=data)
      resp=urllib2.urlopen(req)
      ans=resp.read()
      ans=json.loads(ans)
      token=ans['access_token']
      full_token="Bearer "+token
      headers = {
        "Content-Type": "application/json",
        "Authorization": full_token
    }
      payload = json.dumps(payload)
      response = requests.post(
        service_url,
        data=payload,
        headers=headers
    )
      print response.text
      return response
    
    
    
    if __name__ == '__main__':
        port = int(os.getenv('PORT', 5000))
    
        print "Starting app on port %d" % port
    
        app.run(debug=True, port=port, host='0.0.0.0')
    

    发布回复后,我收到的回复为 200,但在网络聊天中没有收到回复。
    这是我在 python 代码中得到的响应
    {
      "id":"b5b52b9e464b4e958b1219dadedfffce|0000003"
    }
    

    这里有什么问题?完全没有反应

    enter image description here

    得到了这个实现的引用。
    https://chatbotslife.com/microsoft-bot-framework-on-a-bottle-13fdcc3e04e

    最佳答案

    因为,据我所知,基于 skype bot sdk(基于 dotnet)的应用程序应该上传到 azure,然后添加到 skype,它会正常工作。这个 sdk 与 microsoft skype 服务器一起处理请求和响应。
    看这个视频它应该可以帮助你“https://iulia-codes.github.io/notes/guides/build-your-own-skype-bot-with-these-simple-steps/
    我想看到这个库简化了它与 python 一起使用“https://github.com/denissa4/skype_chatbot

    关于python - 如何使用 python 和 bot 框架创建一个简单的 Skype 机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53432607/

    相关文章:

    python - 如何在python中生成具有两个节点之间的长最短路径的有向无环图

    skype - 禁止 Skype 在电话号码旁边显示图标

    html - 通过 Skype4com 发送 html 消息

    c# - BOT 在内部预置环境中使用 MS Bot Framework

    botframework - 即使 Bot ConversationData 属性只设置一次,它们也会被覆盖吗?

    php - 使用 PHP 通过 Skype 发送短信

    python - 按键对列表进行排序并以与第一个列表相同的方式对另一个列表进行排序?

    python - 配置 Pyscripter 以管理不同的 python 发行版(Anaconda 和 ArcGIS)

    python - 带有 Python 2.6 的 Anaconda Python

    Azure Bot 关联语言理解 (LUIS) 应用程序未显示