Last active
October 28, 2022 07:28
-
-
Save lxchurbakov/de1e63a80751af509b2310aad74bddce to your computer and use it in GitHub Desktop.
chatbots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const app = new App({ | |
socketMode: true, | |
token: String(process.env.SLACK_BOT_TOKEN), | |
appToken: String(process.env.SLACK_APP_TOKEN), | |
signingSecret: String(process.env.SLACK_SIGNING_SECRET), | |
}); | |
;(async () => { | |
// Start your app | |
app.start().catch((error) => { | |
console.log(error); | |
process.exit(1); | |
}) | |
})(); | |
// app.action('export', async ({ say, ack, body }) => { | |
// await ack(); | |
// const pdf_file_content = await ejspdf.ejs(path.resolve(__dirname, '../templates/example'), 8001, { | |
// fields: invoiceData, | |
// }); | |
// const { file } = await app.client.files.upload({ | |
// file: pdf_file_content, | |
// filename: 'invoice.pdf', | |
// filetype: 'pdf', | |
// channels: 'C03LZ9NDRPG', | |
// }); | |
// await say(`Invoice exported!`); | |
// }); | |
// app.action(/enter_value_(.*)/, async ({ ack, body, context }) => { | |
// await ack(); | |
// const data = body.actions[0]?.value; | |
// const key = context.actionIdMatches[1]; | |
// invoiceData[key] = data; | |
// }); | |
// { | |
// "type": "input", | |
// "dispatch_action": true, | |
// "element": { | |
// "dispatch_action_config": { | |
// "trigger_actions_on": [ | |
// "on_character_entered" | |
// ] | |
// }, | |
// "type": "plain_text_input", | |
// "action_id": `enter_value_${key}`, | |
// }, | |
// "label": { | |
// "type": "plain_text", | |
// "text": field.label, | |
// "emoji": true | |
// } | |
// } | |
// { | |
// "type": "section", | |
// "text": { | |
// "type": "mrkdwn", | |
// "text": `Hey there <@${message.user}>!` | |
// }, | |
// "accessory": { | |
// "type": "button", | |
// "text": { | |
// "type": "plain_text", | |
// "text": "Click Me" | |
// }, | |
// "action_id": "button_click" | |
// } | |
// } | |
// { | |
// "channel": "CBR2V3XEX", | |
// "attachments": [ | |
// { | |
// "fallback": "Plain-text summary of the attachment.", | |
// "color": "#2eb886", | |
// "pretext": "Optional text that appears above the attachment block", | |
// "author_name": "Bobby Tables", | |
// "author_link": "http://flickr.com/bobby/", | |
// "author_icon": "http://flickr.com/icons/bobby.jpg", | |
// "title": "Slack API Documentation", | |
// "title_link": "https://api.slack.com/", | |
// "text": "Optional text that appears within the attachment", | |
// "fields": [ | |
// { | |
// "title": "Priority", | |
// "value": "High", | |
// "short": false | |
// } | |
// ], | |
// "image_url": "http://my-website.com/path/to/image.jpg", | |
// "thumb_url": "http://example.com/path/to/thumb.png", | |
// "footer": "Slack API", | |
// "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png", | |
// "ts": 123456789 | |
// } | |
// ] | |
// } | |
// app.message('hello', async ({ message, say }) => { | |
// await say({ | |
// blocks: [ | |
// { | |
// "type": "section", | |
// "text": { | |
// "type": "mrkdwn", | |
// "text": `Hey there <@${message.user}>!` | |
// }, | |
// "accessory": { | |
// "type": "button", | |
// "text": { | |
// "type": "plain_text", | |
// "text": "Click Me" | |
// }, | |
// "action_id": "button_click" | |
// } | |
// } | |
// ], | |
// text: `Hey there <@${message.user}>!` | |
// }); | |
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { config } = require('dotenv'); | |
config(); | |
const TelegramBot = require('node-telegram-bot-api'); | |
const bot = new TelegramBot(String(process.env.TELEGRAM_BOT_TOKEN), { polling: true }); | |
// Simple ping pong style messaging | |
bot.on('message', (message) => { | |
if (message.text === 'hey') { | |
bot.sendMessage(message.chat.id, 'Hey yourself!'); | |
} | |
}); | |
// Message with callback_query | |
bot.on('message', async ($) => { | |
if ($.text === 'callback-query') { | |
const message = await bot.sendMessage($.chat.id, 'Let\'s see how callback data works', { | |
reply_markup: { | |
inline_keyboard: [ | |
[{ | |
text: 'Show me a random number!', callback_data: Math.floor(Math.random() * 100), | |
}], | |
], | |
}, | |
}); | |
bot.once('callback_query', (data) => { | |
if (data.message.message_id === message.message_id) { | |
bot.sendMessage(data.message.chat.id, `Your random number is **${data.data}**`); | |
bot.deleteMessage(message.chat.id, message.message_id); | |
} | |
}); | |
} | |
}); | |
// Simple flow (one question after another) | |
const questions = [ | |
{ id: 'name', label: 'What is your name?', type: 'text' }, | |
{ id: 'gender', label: 'What is your gender?', type: 'options', options: [{ value: 'male', label: 'Male' }, { value: 'Female', label: 'female' }] }, | |
{ id: 'age', label: 'How old are you?', type: 'number' }, | |
]; | |
bot.on('message', async (message) => { | |
if (message.text === 'flow') { | |
bot.sendMessage(message.chat.id, 'Let\'s fill in some information!'); | |
const result = await questions.reduce(($, question) => { | |
return $.then((interm) => Promise.resolve(({ | |
'text': () => { | |
return new Promise((resolve, reject) => { | |
bot.once('message', (m) => { | |
resolve(m.text); | |
}); | |
bot.sendMessage(message.chat.id, question.label); | |
}); | |
}, | |
'number': () => { | |
return new Promise((resolve, reject) => { | |
bot.once('message', (m) => { | |
resolve(Number(m.text)); | |
}); | |
bot.sendMessage(message.chat.id, question.label); | |
}); | |
}, | |
'options': () => { | |
return new Promise(async (resolve, reject) => { | |
let m = await bot.sendMessage(message.chat.id, question.label, { | |
reply_markup: { | |
inline_keyboard: [ | |
question.options.map((option) => ({ | |
text: option.label, callback_data: option.value | |
})), | |
], | |
}, | |
}); | |
bot.once('callback_query', ($) => { | |
if ($.message.message_id === m.message_id) { | |
resolve($.data); | |
} | |
}); | |
}); | |
}, | |
}[question.type])()).then((answer) => { | |
return { ...interm, [question.id]: answer }; | |
})); | |
}, Promise.resolve({})); | |
bot.sendMessage(message.chat.id, JSON.stringify(result)); | |
} | |
}); | |
// This does not work well on telegram destkop, also maybe the following command is required | |
// bot.setChatMenuButton({ chat_id: message.chat.id, menu_button: { type: 'commands' }}); | |
bot.on('message', async (message) => { | |
if (message.text === '/start' || message.text === '/leave') { | |
bot.setMyCommands([ | |
{description: 'Enter Admin Mod', command: '/admin' }, | |
]); | |
} | |
if (message.text === '/admin') { | |
bot.setMyCommands([ | |
{description: 'Leave Admin Mod', command: '/leave' }, | |
]); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment