-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (53 loc) · 1.7 KB
/
index.js
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
const config = require('./config');
const StaticNumbers = require('./StaticNumbers');
const Telegraf = require('telegraf')
// replace the value below with the Telegram token you receive from @BotFather
const token = config.botToken;
const bot = new Telegraf(token);
var staticCache = new StaticNumbers(); // Initialize Static numbers.
var sendMessage = function(ctx, message) {
ctx.replyWithHTML(message);
};
var sendHelp = function(ctx) {
var helpMessage = "Bot programmed to be inline. Please start a message with @aczbot";
sendMessage(ctx, helpMessage);
};
bot.help((ctx) => sendHelp(ctx));
bot.on('message', (msg) => {
console.log('Message Received');
});
bot.on("inline_query", async ctx => {
console.log(`Searching for ${ctx.inlineQuery.query}`);
var searchResults = staticCache.search(ctx.inlineQuery.query);
const result = [];
var resultsCounter = 0;
searchResults.some(function(value, index) {
resultsCounter ++;
if(resultsCounter >= 50) {
console.log('Only 50 records can be returned. Make your search string shorter')
return true;
}
var generateMessageText = function(eachObject) {
var returnString = "";
// TODO: Keep this formatted at cache time to avoid formatting at runtime
Object.keys(eachObject).map(function(key) {
returnString += key + ": " + eachObject[key] + "\n";
});
return returnString;
}
result.push({
type: 'article',
title: value.Contact,
id: value.Contact,
input_message_content: {
message_text: generateMessageText(value)
}
}
);
});
// console.log(result);
ctx.answerInlineQuery(result, {
cache_time: 0,
});
});
bot.launch();