-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.bal
91 lines (67 loc) · 2.36 KB
/
service.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import ballerina/http;
import ballerina/io;
import wso2/twitter;
import ballerina/config;
import ballerinax/docker;
endpoint twitter:Client twitter {
clientId: config:getAsString("consumerKey"),
clientSecret: config:getAsString("consumerSecret"),
accessToken: config:getAsString("accessToken"),
accessTokenSecret: config:getAsString("accessTokenSecret")
};
@docker:Expose {}
endpoint http:Listener listener {
port:9090
};
// Docker configurations
@docker:Config {
registry:"registry.hub.docker.com",
name:"helloworld",
tag:"v1.0"
}
@docker:CopyFiles {
files:[
{source:"./twitter.toml", target:"/home/ballerina/conf/twitter.toml", isBallerinaConf:true}
]
}
@http:ServiceConfig {
basePath: "/"
}
service<http:Service> botOp bind listener {
@http:ResourceConfig {
methods: ["POST"],
path: "/"
}
sayHello (endpoint caller, http:Request request) {
json opreq = check request.getJsonPayload();
string searchKeyWord = opreq.keyword.toString();
string lastTweetID = opreq.lastTweetID.toString();
http:Response response = new;
var tweetResponse = twitter->search (searchKeyWord);
match tweetResponse {
twitter:Status[] twitterStatus =>{
string tempTweetID = <string> twitterStatus[0].id;
foreach i in twitterStatus{
int tweetId = i.id;
if ((<string> tweetId)== lastTweetID){
break;
}
string text = i.text;
string source = i.source;
string retweetCount = <string> i.retweetCount;
io:println("Tweet ID: " + <string> tweetId);
io:println("Tweet: " + text);
io:println("Source: " + source);
io:println("RT count: " + retweetCount);
io:println("----------------------------------------------------------------------------");
var tweetResponse1 = twitter->retweet(tweetId);
}
lastTweetID = tempTweetID;
json payload = {latestTweetID:lastTweetID};
response.setJsonPayload(payload);
_ = caller->respond(response);
}
twitter:TwitterError e => io:println(e);
}
}
}