-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatdaddy.js
180 lines (161 loc) · 7.8 KB
/
chatdaddy.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
const {makeAccessTokenFactory, Scope, BotsApi} = require('@chatdaddy/client');
const config = require('config');
const teamId = config.get('CHAT_DADDY_TEAM_ID')
const chatDaddyRefreshToken = config.get('CHAT_DADDY_REFRESH_TOKEN')
// import axios from "axios";
// create a factory that takes care of auto-renewing access tokens when they expire
// (async() => {
module.exports.getChatDaddyToken = async (req, res, next) => {
// console.log('in getChatDaddyToken')
// team ID you want to generate the token for
// read the section below to see how to get your team ID
let chatDaddyToken = null
// console.log('getChatDaddyToken')
const getToken = makeAccessTokenFactory({
request: {
refreshToken: chatDaddyRefreshToken, // example, use your own refresh token
scopes: [Scope.MessagesSendToAll, Scope.AccountCreate, Scope.AccountRead,
Scope.ChatsAccessAll, Scope.AccountPatch, Scope.AccountDelete,
Scope.ContactsReadAll, Scope.ContactsReadAssigned, Scope.MessagesDelete,
Scope.ChatsDelete, Scope.ChatdaddyHook, Scope.MessagesSchedule,
Scope.MessagesSendToAssigned, Scope.TagsCreate,
Scope.TagsRead, Scope.TagsDelete, Scope.TagsCreate, Scope.TagsRead, Scope.TagsDelete,
Scope.ContactsCreate, Scope.ContactsDelete, Scope.ContactsUpdate,Scope.KeywordRead,
Scope.KeywordCreate, Scope.KeywordDelete, Scope.KeywordUpdate, Scope.TemplatesRead,
Scope.TemplatesDelete, Scope.TemplatesUpdate, Scope.TemplatesCreate, Scope.CampaignsCreate,
Scope.CampaignsDelete, Scope.CampaignsUpdate, Scope.CampaignsRead],
}});
const token = await getToken(teamId)
// console.log(token);
// above line would print something like "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
// send using WA API
// var options = {
// method: 'POST',
// url: 'https://api-im.chatdaddy.tech/messages/acc_205a7b2a-add0-4bb5-a6_0f9e/[email protected]',
// headers: {
// 'Content-Type': 'application/json',
// Authorization: `Bearer ${token.token}`
// },
// data: {text: 'chatdaddy Api', buttons:[{
// text:"test button",
// id:"123"
// },]}
// };
// axios.request(options).then(function (response) {
// console.log(response.data);
// }).catch(function (error) {
// console.error(error);
// });
// const options = {
// method: 'POST',
// url: 'https://api-im.chatdaddy.tech/accounts',
// headers: {'Content-Type': 'application/json', Authorization: `Bearer ${token.token}`},
// data: {type: 'wa', tier: 'limited_msg_no_chat_history', nickname: 'adab'}
// };
//
// axios.request(options).then(function (response) {
// console.log(response.data);
// }).catch(function (error) {
// console.error(error);
// });
// axios.post(
// 'https://api-im.chatdaddy.tech/messages/[email protected]',
// {
// text: 'Hi there!'
// },
// {
// headers: {
// 'authorization': `Bearer ${token}`,
// 'content-type': 'application/json'
// },
// }
// ).then(e => console.log(e))
// .catch(err => console.log(err))
global.chatDaddyToken = token.token
req.chatDaddyToken = token.token
// console.log('Chat Daddy ----->', req.chatDaddyToken)
// socket.connect
// await config.set("CHAT-DADDY-TOKEN", chatDaddyToken)
// console.log(config.get("CHAT-DADDY-TOKEN"))
next()
}
module.exports.extractChatDaddyToken = async () => {
// console.log('in getChatDaddyToken')
// team ID you want to generate the token for
// read the section below to see how to get your team ID
let chatDaddyToken = null
// console.log('getChatDaddyToken')
const getToken = makeAccessTokenFactory({
request: {
refreshToken: chatDaddyRefreshToken, // example, use your own refresh token
scopes: [Scope.MessagesSendToAll, Scope.AccountCreate, Scope.AccountRead,
Scope.ChatsAccessAll, Scope.AccountPatch, Scope.AccountDelete,
Scope.ContactsReadAll, Scope.ContactsReadAssigned, Scope.MessagesDelete,
Scope.ChatsDelete, Scope.ChatdaddyHook, Scope.MessagesSchedule,
Scope.MessagesSendToAssigned, Scope.TagsCreate,
Scope.TagsRead, Scope.TagsDelete, Scope.TagsCreate, Scope.TagsRead, Scope.TagsDelete,
Scope.ContactsCreate, Scope.ContactsDelete, Scope.ContactsUpdate, Scope.KeywordRead,
Scope.KeywordCreate, Scope.KeywordDelete, Scope.KeywordUpdate, Scope.TemplatesRead,
Scope.TemplatesDelete, Scope.TemplatesUpdate, Scope.TemplatesCreate, Scope.CampaignsCreate,
Scope.CampaignsDelete, Scope.CampaignsUpdate, Scope.CampaignsRead],
// scopes: [Scope.MessagesSendToAll, Scope.AccountCreate, Scope.AccountRead,
// Scope.ChatsAccessAll, Scope.AccountPatch, Scope.AccountDelete,
// Scope.ContactsReadAll, Scope.ContactsReadAssigned, Scope.NotificationRead, Scope.MessagesDelete,
// Scope.ChatsDelete, Scope.ChatdaddyHook, Scope.TemplatesCreate, Scope.TemplatesDelete,
// Scope.TemplatesRead, Scope.TemplatesUpdate, Scope.KeywordRead, Scope.KeywordCreate,
// Scope.KeywordDelete, Scope.KeywordUpdate, Scope.MessagesSchedule,
// Scope.MessagesSendToAssigned, Scope.CampaignsCreate, Scope.TagsCreate,
// Scope.TagsRead, Scope.TagsDelete, Scope.TagsCreate, Scope.TagsRead, Scope.TagsDelete,
// Scope.ContactsCreate, Scope.ContactsDelete, Scope.ContactsUpdate], // only add scopes to send messages
},
});
const token = await getToken(teamId);
// console.log(token);
// above line would print something like "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
// send using WA API
// var options = {
// method: 'POST',
// url: 'https://api-im.chatdaddy.tech/messages/acc_205a7b2a-add0-4bb5-a6_0f9e/[email protected]',
// headers: {
// 'Content-Type': 'application/json',
// Authorization: `Bearer ${token.token}`
// },
// data: {text: 'chatdaddy Api', buttons:[{
// text:"test button",
// id:"123"
// },]}
// };
// axios.request(options).then(function (response) {
// console.log(response.data);
// }).catch(function (error) {
// console.error(error);
// });
// const options = {
// method: 'POST',
// url: 'https://api-im.chatdaddy.tech/accounts',
// headers: {'Content-Type': 'application/json', Authorization: `Bearer ${token.token}`},
// data: {type: 'wa', tier: 'limited_msg_no_chat_history', nickname: 'adab'}
// };
//
// axios.request(options).then(function (response) {
// console.log(response.data);
// }).catch(function (error) {
// console.error(error);
// });
// axios.post(
// 'https://api-im.chatdaddy.tech/messages/[email protected]',
// {
// text: 'Hi there!'
// },
// {
// headers: {
// 'authorization': `Bearer ${token}`,
// 'content-type': 'application/json'
// },
// }
// ).then(e => console.log(e))
// .catch(err => console.log(err))
global.chatDaddyToken = token.token
// console.log('Chat Daddy ----->', global.chatDaddyToken)
}
// })()