-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
182 lines (122 loc) · 4.42 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const Telegraf = require('telegraf')
const session = require('telegraf/session')
const RedisSession = require('telegraf-session-redis')
const fs = require('fs')
//const json = require('./data.json')
require('dotenv').config()
const bot = new Telegraf(process.env.TELEGRAF_API_KEY)
bot.use(session())
function UserSession(userInfo)
{
userInfo.session.counter = userInfo.session.counter || 0
userInfo.session.counter++
//console.log(userInfo.from)
console.log(userInfo.session.counter)
// console.log(userInfo.message.from.id)
console.log(JSON.stringify(userInfo.message, null, 2))
fs.appendFile('./data.json',JSON.stringify(userInfo.message, null, 2), (err)=>{
if(err){
console.log(err);
}
else{
console.log('File succesfully writtern!')
}
})
}
bot.start((ctx) => {
ctx.reply("Hello "+ getName(ctx.from)+"! \nWecome to Telegram TechInt Bot")
ctx.reply("What can this bot do? \n\nYou can use these commands to control me: \n/start - Start the bot\n/Questions - Ask me questions about myself.\n/currentUser - Some information about the current user.\n/help - Show this help page.")
UserSession(ctx)
})
bot.help((ctx) => {
ctx.reply("Command reference:\n/start - Start the bot\n/Questions - Ask me questions about myself.\n/currentUser - Some information about the current user.\n/help - Show this help page.")
})
bot.on('sticker',(ctx) => {
ctx.reply("Cool Sticker")
})
//===============Function=====================
function getName(user) {
return user.first_name
}
bot.hears('hello', (ctx) => {
ctx.reply("Hello Sir, How are you ")
})
bot.command('currentUser', (ctx) => {
ctx.reply("Name: "+ctx.message.from.first_name +"\nLast Name: "+ctx.message.from.last_name+"\nLanguage code: "+ctx.message.from.language_code)
console.log(ctx.message)
})
//============== Interactive menu --Asking Questions ======================
bot.command('Questions',(ctx) => {
UserSession(ctx)
bot.telegram.sendMessage(ctx.chat.id, "What would you like to know?",
{
reply_markup: {
inline_keyboard: [
[
{text: 'Who created you?', callback_data: 'creator'}
],
[
{text: 'When were you created?', callback_data: 'creation_date'}
],
[
{text: 'Where can I find your source code?', callback_data: 'source_code'}
]
]
}
})
})
bot.action('creator', (ctx) =>{
bot.telegram.sendMessage(ctx.chat.id, 'I was created by Velisa Nomfula',
{
reply_markup: {
inline_keyboard: [
[
{text: 'Go back to the menu?', callback_data: 'menu'}
]
]
}
});
})
bot.action('creation_date', (ctx) =>{
bot.telegram.sendMessage(ctx.chat.id, 'I was created on the 17st of March 2021',
{
reply_markup: {
inline_keyboard: [
[
{text: 'Go back to the menu?', callback_data: 'menu'}
]
]
}
});
})
bot.action('source_code', (ctx) =>{
bot.telegram.sendMessage(ctx.chat.id, '-You can find my source code from https://github.com/VelisaNomfula/ GitHub repository \n-The name of the repository is called aweson_velisa_bot \n-The README.md file is also provided to guide you on how to use me',
{
reply_markup: {
inline_keyboard: [
[
{text: 'Go back to the menu?', callback_data: 'menu'}
]
]
}
});
})
bot.action('menu',(ctx) => {
bot.telegram.sendMessage(ctx.chat.id, "What would you like to know?",
{
reply_markup: {
inline_keyboard: [
[
{text: 'Who created you?', callback_data: 'creator'}
],
[
{text: 'When were you created?', callback_data: 'creation_date'}
],
[
{text: 'Where can I find your source code?', callback_data: 'source_code'}
]
]
}
})
})
bot.launch()