How to create a simple skype bot with python and bot framework
I am trying to create a very simple bot in skype with botframework using python.
Here are the steps i did.
- Created a channels bot in resources.
- Created a simple python web service using flask to receive the message from the bot. Exposed it over ngrok and added the same in the messaging endpoint.
I tested the bot from web chat and i am getting a json in my python webservice
{
"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"
}
I am able to process this json and able to get the user input "hello from test" in this example to my webservice.
What i want to do is to return the same to my web chat bot from python.
I referred the below tutorial as well. I am able to post response back but i am not getting it in the bot.
python code of backend as below
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')
I am getting the response as 200 once i post the response , but not getting the response in webchat.
This is the response i get in python code
{
"id":"b5b52b9e464b4e958b1219dadedfffce|0000003"
}
What is wrong here ? No response at all
Got this reference for this implementation.
https://chatbotslife.com/microsoft-bot-framework-on-a-bottle-13fdcc3e04e
python botframework skype
add a comment |
I am trying to create a very simple bot in skype with botframework using python.
Here are the steps i did.
- Created a channels bot in resources.
- Created a simple python web service using flask to receive the message from the bot. Exposed it over ngrok and added the same in the messaging endpoint.
I tested the bot from web chat and i am getting a json in my python webservice
{
"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"
}
I am able to process this json and able to get the user input "hello from test" in this example to my webservice.
What i want to do is to return the same to my web chat bot from python.
I referred the below tutorial as well. I am able to post response back but i am not getting it in the bot.
python code of backend as below
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')
I am getting the response as 200 once i post the response , but not getting the response in webchat.
This is the response i get in python code
{
"id":"b5b52b9e464b4e958b1219dadedfffce|0000003"
}
What is wrong here ? No response at all
Got this reference for this implementation.
https://chatbotslife.com/microsoft-bot-framework-on-a-bottle-13fdcc3e04e
python botframework skype
Are you saying your bot is able to receive messages but not send them?
– Kyle Delaney
Nov 27 '18 at 1:49
Do you still need help?
– Kyle Delaney
Dec 4 '18 at 3:56
add a comment |
I am trying to create a very simple bot in skype with botframework using python.
Here are the steps i did.
- Created a channels bot in resources.
- Created a simple python web service using flask to receive the message from the bot. Exposed it over ngrok and added the same in the messaging endpoint.
I tested the bot from web chat and i am getting a json in my python webservice
{
"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"
}
I am able to process this json and able to get the user input "hello from test" in this example to my webservice.
What i want to do is to return the same to my web chat bot from python.
I referred the below tutorial as well. I am able to post response back but i am not getting it in the bot.
python code of backend as below
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')
I am getting the response as 200 once i post the response , but not getting the response in webchat.
This is the response i get in python code
{
"id":"b5b52b9e464b4e958b1219dadedfffce|0000003"
}
What is wrong here ? No response at all
Got this reference for this implementation.
https://chatbotslife.com/microsoft-bot-framework-on-a-bottle-13fdcc3e04e
python botframework skype
I am trying to create a very simple bot in skype with botframework using python.
Here are the steps i did.
- Created a channels bot in resources.
- Created a simple python web service using flask to receive the message from the bot. Exposed it over ngrok and added the same in the messaging endpoint.
I tested the bot from web chat and i am getting a json in my python webservice
{
"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"
}
I am able to process this json and able to get the user input "hello from test" in this example to my webservice.
What i want to do is to return the same to my web chat bot from python.
I referred the below tutorial as well. I am able to post response back but i am not getting it in the bot.
python code of backend as below
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')
I am getting the response as 200 once i post the response , but not getting the response in webchat.
This is the response i get in python code
{
"id":"b5b52b9e464b4e958b1219dadedfffce|0000003"
}
What is wrong here ? No response at all
Got this reference for this implementation.
https://chatbotslife.com/microsoft-bot-framework-on-a-bottle-13fdcc3e04e
python botframework skype
python botframework skype
edited Nov 23 '18 at 7:06
lost Coder
asked Nov 22 '18 at 13:59
lost Coderlost Coder
159221
159221
Are you saying your bot is able to receive messages but not send them?
– Kyle Delaney
Nov 27 '18 at 1:49
Do you still need help?
– Kyle Delaney
Dec 4 '18 at 3:56
add a comment |
Are you saying your bot is able to receive messages but not send them?
– Kyle Delaney
Nov 27 '18 at 1:49
Do you still need help?
– Kyle Delaney
Dec 4 '18 at 3:56
Are you saying your bot is able to receive messages but not send them?
– Kyle Delaney
Nov 27 '18 at 1:49
Are you saying your bot is able to receive messages but not send them?
– Kyle Delaney
Nov 27 '18 at 1:49
Do you still need help?
– Kyle Delaney
Dec 4 '18 at 3:56
Do you still need help?
– Kyle Delaney
Dec 4 '18 at 3:56
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432607%2fhow-to-create-a-simple-skype-bot-with-python-and-bot-framework%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432607%2fhow-to-create-a-simple-skype-bot-with-python-and-bot-framework%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you saying your bot is able to receive messages but not send them?
– Kyle Delaney
Nov 27 '18 at 1:49
Do you still need help?
– Kyle Delaney
Dec 4 '18 at 3:56