Skip to content

Instantly share code, notes, and snippets.

@winse
Created September 28, 2016 01:09
Show Gist options
  • Save winse/78fa84dc6e062d24d0b8ffb1043997af to your computer and use it in GitHub Desktop.
Save winse/78fa84dc6e062d24d0b8ffb1043997af to your computer and use it in GitHub Desktop.
bookofanswers
using cn.bmob.api;
using cn.bmob.io;
using cn.bmob.json;
using cn.bmob.tools;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BmobTest.answers
{
class Answers : BmobTable
{
public BmobInt index { get; set; }
public string answer { get; set; }
public override void readFields(BmobInput input)
{
base.readFields(input);
this.index = input.getInt("index");
this.answer = input.getString("answer");
}
public override void write(BmobOutput output, Boolean all)
{
base.write(output, all);
output.Put("index", this.index);
output.Put("answer", this.answer);
}
public static BmobWindows server()
{
var Bmob = new BmobWindows();
Bmob.initialize("5c4db3dcd0d2c259a82e3cd90b9a1921", "1bc16364ee4620d8b25c701dd16a3958");
BmobDebug.Register(msg => { Debug.WriteLine(msg); });
BmobDebug.level = BmobDebug.Level.TRACE;
return Bmob;
}
public static void load(String path)
{
var Bmob = server();
StreamReader reader = null;
try
{
reader = new StreamReader(path);
int count = 0;
string line = null;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length == 0) {
continue;
}
count++;
var data = new Answers();
data.index = count;
data.answer = line;
var future = Bmob.CreateTaskAsync("answers", data);
var resp = future.Result;
Console.WriteLine();
Console.WriteLine("\n返回结果: " + JsonAdapter.JSON.ToDebugJsonString(resp));
}
}
finally
{
if (reader != null)
{
reader.Close();
}
}
}
}
}
your actions will\n improve things
don't bet on it
adopt\n an adventurous attitude
follow the advice of experts
you could find yourself\n unable to compromise
focus on your home life
investigate and then enjoy it
definitely
it will remain unpredictable
absolutely not
explore it\n with playful curiosity
be delightfully sure of it
better to wait
it seems assured
do it early
keep it to yourself
startling events\n may occur as a result
the answer may come to you\n in another language
you will need\n to accommodate
doubt it
it will bring good luck
be patient
you will find out everything\n you'll need to know
there is a substantial link\n to another situation
watch and see what happens
it will affect\n how others see you
you'll be happy you did
get it in writing
unfavorable at this time
upgrade any way you can
if you do as you're told
if it's done well;\n if not, don't do it at all
don't ask for any more\n at this time
avoid the first solution
you'll get the final word
proceed at a more\n relaxed pace
the best solution\n may not be the obvious one
remain flexible
that's out of your control
provided you say\n "thank you"
enjoy the experience
approach cautiously
pay attention to the details
watch your step as you go
speak up about it
don't hesitate
this is a good time to\n make a new plan
move on
there is no guarantee
the circumstances\n will change very quickly
don't get caught up\n in your emotions
shift your focus
it is significant
reprioritize what is important
make a list of why not
don't wait
it is something you won't forget
expect to settle
seek out more options
follow through\n on your obligations
deal with it later
follow someone else's lead
make a list of why
take a chance
accept a change to your routine
you'll need to take\n the initiative
you'll have to compromise
you'll need\n more information
trust\n your original thought
it will create a stir
remove your own obstacles
it would be better\n to focus on your work
it will be a pleasure
be more generous
bet on it
finish something else first
you may have opposition
you are too close to see
the situation is unclear
a substantial effort\n will be required
allow yourself to rest first
the chance will not\n come again soon
reconsider your approach
it would be inadvisable
wait for a better offer
settle it soon
yes,\n but don't force it
get a clearer view
take a chance
now you can
don't overdo it
it will sustain you
it'll cost you
it is sure to make things\n interesting
be practical
save your energy
it is certain
it is uncertain
the outcome will be positive
no matter what
you may have to drop\n other things
don't be concerned
prepare for the unexpected
it is not significant
tell someone\n what it means to you
whatever you do\n the results will be lasting
keep an open mind
it's a good time\n to make plans
it may be difficult\n but you will find value in it
it is worth the trouble
there will be obstacles\n to overcome
related issues may surface
you are sure to have support
assistance would make\n your progress\n a success
collaboration\n will be the key
seek out more options
take charge
it cannot fail
you must act now
respect the rules
gentle persistence\n will pay off
you will not be disappointed
it may already be\n a done deal
follow through\n with your good intentions
take more time to decide
don't be pressured\n into acting too quickly
don't ignore the obvious
if you don't resist
it's not worth a struggle
don't forget to have fun
don't doubt it
a strong commitment\n will achieve good results
try a more unlikely solution
leave behind old solutions
not if you're alone
mishaps are highly probable
press for closure
realize that\n too many choices is as\n difficult as too few
yes
listen more carefully;\n then you will know
the answer\n is in your backyard
laugh about it
others will depend\n on your choices
let it go
that would be a waste\n of money
give it all you've got
you don't really care
you'll need to\n consider other ways
a year from now\n it won't matter
don't waste your time
it could be extraordinary
count to 10;\n ask again
act as though\n it is already real
setting priorities\n will be a necessary part\n of the process
use your imagination
it's gonna be great
to ensure the best decision,\n be calm
wait
you'll have to make it up\n as you go
you'll regret it
unquestionably
of course
you know better now\n than ever before
trust your intuition
consider it an opportunity
ask your father
never
ask your mother
perhaps, when you're older
only do it once
maybe
no
yes
your actions will\n improve things
don't be ridiculous
don't bet on it
adopt\n an adventurous attitude
follow the advice of experts
you could find yourself\n unable to compromise
focus on your home life
investigate and then enjoy it
definitely
it will remain unpredictable
absolutely not
explore it\n with playful curiosity
be delightfully sure of it
better to wait
it seems assured
do it early
keep it to yourself
startling events\n may occur as a result
the answer may come to you\n in another language
you will need to\n accommodate
doubt it
it will bring good luck
be patient
you will find out everything\n you'll need to know
there is a substantial link\n to another situation
watch and see what happens
it will affect\n how others see you
you'll be happy you did
get it in writing
unfavorable at this time
upgrade any way you can
if you do as you're told
if it's done well;\n if not, don't do it at all
don't ask for any more\n at this time
avoid the first solution
you'll get the final word
proceed at a more\n relaxed pace
the best solution\n may not be the obvious one
remain flexible
that's out of your control
provided you say\n"thank you"
enjoy the experience
approach cautiously
pay attention to the details
watch your step as you go
speak up about it
don't hesitate
this is a good time to\n make a new plan
move on
there is no guarantee
the circumstances\n will change very quickly
don't get caught up\n in your emotions
shift your focus
it is significant
reprioritize what is important
make a list of why not
don't wait
there is good reason\n to be optimistic
it is something\n you won't forget
no
seek out more options
follow through\n on your obligations
deal with it later
follow someone else's lead
make a list of why
take a chance
accept a change\n to your routine
you'll need to take the initiative
you'll have to compromise
you'll need more information
trust\n your original thought
it will create s stir
remove your own obstacles
it would be better\n to focus on your work
it will be a pleasure
be more generous
bet on it
mishaps are highly probable
press for closure
realize that\n too many choices is as\n difficult as too few
you must
listen more carefully;\n then you will know
the answer\n is in your backyard
laugh about it
others will depend\n on your choices
let it go
that would be a waste\n of money
it's time for you to go
give it all you've got
you don't really care
you'll need to\n consider other ways
a year from now\n it won't matter
don't waste your time
it could be extraordinary
count to 10;\n ask again
act as though\n it is already real
setting priorities\n will be a necessary part\n of the process
use your imagination
it's gonna be great
to ensure the best decision,\n be calm
wait
you'll have to make it up\n as you go
you'll regret it
unquestionably
of course
you know better now\n than ever before
trust your intuition
consider it an opportunity
ask your father
never
ask your mother
perhaps, when you're older
only do it once
maybe
no
yes
finish something else first
you may have opposition
you are too close to see
the situation is unclear
a substantial effort\n will be required
allow yourself to rest first
the chance will not\n come again soon
reconsider your approach
it would be inadvisable
wait for a better offer
settle it soon
yes,\n but don't force it
get a clearer view
take a chance
now you can
don't overdo it
it will sustain you
it'll cost you
it is sure to make things\n interesting
be practical
save your energy
it is certain
it is uncertain
the outcome will be positive
no matter what
you may have to drop\n other things
don't be concerned
prepare for the unexpected
it is not significant
tell someone\n what it means to you
whatever you do\n the results will be lasting
keep an open mind
it's a good time\n to make plans
it may be difficult\n but you will find value in it
it is worth the trouble
there will be obstacles\n to overcome
related issues may surface
you are sure to have support
assistance would make\n your progress\n a success
collaboration will be the key
seek out more options
take charge
it cannot fail
you must act now
respect the rules
gentle persistence\n will pay off
you will be disappointed
it may already be\n a done deal
follow through\n with your good intentions
take more time to decide
don't be pressured\n into acting too quickly
don't ignore the obvious
if you don't resist
it's not worth a struggle
don't forget to have fun
don't doubt it
a strong commitment\n will achieve good results
try a more unlikely solution
leave behind old solutions
not if you're alone
function onRequest(request, response, modules) {
var verify = function () {
var $signature = request.query.signature;
var $timestamp = request.query.timestamp;
var $nonce = request.query.nonce;
var $token = "WEIXIN987654321"; //在微信开发者页面填写的token字符串
var $echostr = request.query.echostr;
var params = [$token, $timestamp, $nonce].sort().join("")
var crypto = modules.oCrypto;
var code = crypto.createHash('sha1').update(params).digest('hex');
var result = ( code == $signature ? $echostr : "Unauthorized" );
return result
}
var responseFunc = function (request, answer, head) {
var xml2js = modules.oXml2js;
if (!head)
head = "ANSWER";
var textAndPic = {
xml: {
ToUserName: request.body.xml.FromUserName,
FromUserName: request.body.xml.ToUserName,
CreateTime: new Date().getTime(),
MsgType: "news",
ArticleCount: 1,
Articles: [
{
item: {
Title: head,
Description: answer,
Url: "http://mp.weixin.qq.com/s?__biz=MjM5NjcwNzgzNw==&mid=310840797&idx=1&sn=ce12607ac4c9bc343192af898a2dd31e#rd"
}
}
]
}
}
var builder = new xml2js.Builder();
var xml = builder.buildObject(textAndPic); //利用模块xml2js,把json对象转换为一个xml文本
response.setHeader('Content-Type', 'text/xml'); //设置返回的http header
response.end(xml);
}
// 接收定阅者发送过来的消息后返回,把反馈意见存储表中。再返回数据。
var saveAndResponse = function () {
var table = "answers";
// answers表记录条数
var num = 349;
var db = modules.oData;
var t = Math.floor(Math.random() * num) + 1
// 查询得到记录具体内容
db.find({
"table": table,
"where": {"index": t}
}, function (err, records) {
var answer = JSON.parse(records).results[0].answer.toUpperCase().replace(/\\N /, "\n");
db.insert(
{
"table": "reqs_bookofanswers", //表名
"data": {
"userId": request.body.xml.FromUserName,
"content": request.body.xml.Content,
"answer": answer
}
},
function (err, data) {
responseFunc(request, answer)
});
});
}
var help = function () {
responseFunc(
request,
"1、用10~15秒想一个(封闭式的)问题,如:这周末去旅行?;\n2、在输入框中输入您的问题;\n3、点击【发送】按钮,再您觉得合适的时间后松开。然后公众号会给您答案。\n\n其实和抛硬币类似,带你找答案而非答案!!",
"提问姿势"
)
}
var httptype = modules.oHttptype;
if ("get" == httptype) {
var resp = verify();
response.end(resp);
} else {
var question = request.body.xml.Content;
if (question) {
question = question.trim().toLowerCase();
if (question.length == 0 || question == "" || question == "h" || question == "help") {
help();
} else {
saveAndResponse();
}
} else {
help();
}
}
}
[TestMethod()]
public void LoadAnswersTest()
{
answers.Answers.load("E:/winsegit/hist/bmob/bmob-cloud-tool/cloud/anwsers.txt");
}
[TestMethod()]
public void FindAnswersTest()
{
var Bmob = answers.Answers.server();
var resp = Bmob.FindTaskAsync<Dictionary<String, Object>>("reqs_bookofanswers", new BmobQuery());
var questions = resp.Result;
Console.WriteLine(questions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment