我正在使用Microsoft Bot Framework制作一个会话天气聊天机器人。
然而,StreamReader对象返回一个空字符串。有人可以帮帮我吗?
Python脚本:
import requests
city = "something"
APIkey = "something"
r = requests.post('http://api.openweathermap.org/data/2.5/weather?q='+city+'&APPID='APIkey)
json_obj = r.json()
print(json_obj['weather'][0]['description'])
当我在python shell中运行此脚本时,我将其作为输出:
haze
MessagesController.cs类
public class MessagesController : ApiController
{
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
// calculate something for us to return
string s = activity.Text;
Weather w = new Weather();
s = w.getWeather(s);
// return our reply to the user
Activity reply = activity.CreateReply(s);
await connector.Conversations.ReplyToActivityAsync(reply);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
}
Weather类
中的getWeather()方法public string getWeather(string s)
{
ProcessStartInfo pythonInfo = new ProcessStartInfo();
Process python;
StreamReader sr;
pythonInfo.FileName = @"somepath\python.exe";
pythonInfo.Arguments = @"somepath\file.py";
pythonInfo.CreateNoWindow = false;
pythonInfo.UseShellExecute = false;
pythonInfo.RedirectStandardOutput = true;
Debug.WriteLine("Python Starting");
python = Process.Start(pythonInfo);
Debug.WriteLine("Python Started");
sr = python.StandardOutput;
Debug.WriteLine("passed standard output to sr");
string text = sr.ReadLine();
Debug.WriteLine(sr.ReadLine());
Debug.WriteLine(text);
python.WaitForExit();
python.Close();
sr.Close();
return text;
}
文本作为空字符串返回,而不是python脚本给出的输出。请帮忙!